2012-01-18 43 views
0

For some reason my simple wordpress query_posts is omitting the latest (first) link from my loop.query_posts WordPress的循环同时省略第一<a href"">

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.

+0

我thiking权,这是无效的HTML? – SMacFadyen 2012-01-18 14:36:33

+0

如果您也可以发布输出HTML,可能会有所帮助。 – 2012-01-18 14:39:38

+0

HTML在那里...... – SMacFadyen 2012-01-18 14:42:21

回答

0

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

如果您在网页上或在模板以外循环中,使用<?php rewind_posts(); ?>以前的循环之后和之前的<?php $my_query = new WP_Query...

+0

这有效,但我的第一个<?php the_title(); ?>没有被包围在标记中。莫名其妙。 – SMacFadyen 2012-01-18 15:22:10

+0

页面或模板上的任何其他循环?如果是这样,使用'<?php rewind_posts(); ?>' – markratledge 2012-01-18 17:22:25