[英]query_posts wordpress loop while is omitting first <a href“”>
由于某种原因,我简单的wordpress query_posts省略了循环中的最新(第一个)链接。
看这里:
<?php query_posts('category_name=blog&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p><?php echo wp_trim_excerpt(); ?></p>
<span class="readmore">
<a href="<?php the_permalink(); ?>">Read more...</a>
</span>
<?php endwhile;?>
它会输出所有预期的内容,但是不会将第一个<a href="">
附加到<a href="">
,例如'Blog Post 2'。
HTML输出
Blog Post 2
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna.</p>
<span class="readmore">
<a href="http://xxx/?p=58">Read more...</a>
</span>
<a href="xxx">Blog Post 1</a>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna.</p>
<span class="readmore">
<a href="http://xxx/?p=55">Read more...</a>
</span>
<a class="more-news" href="">More news..</a>
如您所见,它没有将<Blog Post 2”包装在< <a>
标记中。
尝试一个新查询而不是query_posts,如下所示:
<?php $my_query = new WP_Query('category_name=blog&showposts=2'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p><?php echo wp_trim_excerpt(); ?></p>
<span class="readmore">
<a href="<?php the_permalink(); ?>">Read more...</a>
</span>
<?php endwhile;?>
参见http://codex.wordpress.org/Class_Reference/WP_Query
如果页面或模板中还有其他循环,请使用<?php rewind_posts(); ?>
<?php rewind_posts(); ?>
在上一个循环之后并在<?php $my_query = new WP_Query...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.