简体   繁体   English

query_posts wordpress循环,而首先省略

[英]query_posts wordpress loop while is omitting first <a href“”>

For some reason my simple wordpress query_posts is omitting the latest (first) link from my loop. 由于某种原因,我简单的wordpress query_posts省略了循环中的最新(第一个)链接。

See here: 看这里:

<?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;?>

It's outputting everything expected, however the first <a href=""> is not getting appended to, for example 'Blog Post 2'. 它会输出所有预期的内容,但是不会将第一个<a href="">附加到<a href=""> ,例如'Blog Post 2'。

HTML output 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>

As you can see, it's not wrapping 'Blog Post 2' in a <a> tag. 如您所见,它没有将<Blog Post 2”包装在< <a>标记中。

Try a new query rather than query_posts, like this: 尝试一个新查询而不是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;?>

See http://codex.wordpress.org/Class_Reference/WP_Query 参见http://codex.wordpress.org/Class_Reference/WP_Query

If you have other loops on the page or in the template, use <?php rewind_posts(); ?> 如果页面或模板中还有其他循环,请使用<?php rewind_posts(); ?> <?php rewind_posts(); ?> after the previous loop and right before the <?php $my_query = new WP_Query... <?php rewind_posts(); ?>在上一个循环之后并在<?php $my_query = new WP_Query...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM