簡體   English   中英

query_posts wordpress循環,而首先省略

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM