简体   繁体   中英

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.

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'.

HTML output

   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.

Try a new query rather than query_posts, like this:

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

If you have other loops on the page or in the template, use <?php rewind_posts(); ?> <?php rewind_posts(); ?> after the previous loop and right before the <?php $my_query = new WP_Query...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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