我使用函数来显示博客中最后3篇文章。我希望第一篇帖子显示标题,并用"显示更多"链接修剪帖子内容,其他两篇帖子只显示标题(没有内容),用"显示更"链接。
现在我使用:
<?php query_posts('category_name=blog&showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title"><?php $title = get_the_title(); echo wp_trim_words( $title , '4', $more = null ); ?></p></a>
<div class="post_skrot"><?php echo wp_trim_words( get_the_content(), $num_words = 8, $more = '... <a class="button_more" href="'. get_permalink($post->ID) . '">show more >> </a>' ); ?></div>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
如何修改?
创建一个变量,如果它是第一个post,将返回true
,如果不是,则返回false
:
<?php
//Establish first post check variable
$first_post = true;
query_posts('category_name=blog&showposts=3');
while (have_posts()) : the_post(); ?>
<li>
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title"><?php $title = get_the_title(); echo wp_trim_words( $title , '4', $more = null ); ?></p></a>
<?php if($first_post) { ?>
<div class="post_skrot"><?php echo wp_trim_words( get_the_content(), $num_words = 8, $more = '... <a class="button_more" href="'. get_permalink($post->ID) . '">show more >> </a>' ); ?></div>
<?php } else { ?>
<div class="post_skrot"><a class="button_more" href="'.get_permalink($post->ID).'">show more>> </a></div>
<?php } ?>
</li>
<?php
//Change value of $first_post
$first_post = false;
endwhile;
wp_reset_query(); ?>
在while循环之前添加一个计数器,并将其递增到循环中。如果是1显示内容,其他则不显示(只是标题和链接"…显示更多"):
<?php
query_posts('category_name=blog&showposts=3');
$postCount = 1;
while (have_posts()) :
the_post();
?>
<li>
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title">
<?php
$title = get_the_title();
echo wp_trim_words($title, '4', $more = null);
?>
</p>
</a>
<div class="post_skrot">
<?php
if($postCount == 1){
//displays both content and link 'show more'
echo wp_trim_words(get_the_content(), $num_words = 8, $more = '... <a class="button_more" href="' . get_permalink($post->ID) . '">show more >> </a>');
}else{
//displays only link 'show more'
echo '... <a class="button_more" href="' . get_permalink($post->ID) . '">show more >> </a>';
}
?>
</div>
</li>
<?php
$postCount++;
endwhile;
wp_reset_query();
?>
有了这样的计数器,您可以定义要显示的帖子内容的数量,以及只有标题的帖子数量。
正如@mitkosoft在评论中所说,您希望在while循环中添加一个计数器,以确定这是否是第一次迭代。如果是,则添加附加信息,如果不是,则不添加任何信息。
<?php query_posts('category_name=blog&showposts=3'); ?>
<?php
$counter = 0;
while (have_posts()) : the_post(); ?>
<li>
<a href="<?php echo get_permalink($post->ID); ?>">
<p class="news_title"><?php $title = get_the_title(); echo wp_trim_words( $title , '4', $more = null ); ?></p>
</a>
<?php if($counter < 1):?>
<div class="post_skrot"><?php echo wp_trim_words( get_the_content(), $num_words = 8, $more = '... <a class="button_more" href="'. get_permalink($post->ID) . '">show more >> </a>' ); ?></div>
<?php endif; ?>
</li>
<?php $counter++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
该解决方案的关键部分是添加$counter
、if($counter < 1)
语句和计数器增量$counter++
。如果你决定想要一篇以上带有修剪单词的帖子,你可以增加If语句中的数字,如下所示:if($counter < 5)
,这将为你提供前5篇带有修剪单词。
注意:人们在使用计数器和循环时犯的一个典型错误是不递增计数器,因此请确保包含$counter++
行。
进一步阅读:
- While循环
- If语句
- 替代控制结构(
if($counter < 1): ?>
部分)