the_excerpt not displaying

时间:2013-03-24 作者:Lorenzo

我有一个循环,显示站点上的所有帖子,问题是没有显示\\u摘录。我做错了什么?

  <?php 
    $all_posts = get_posts(array(\'numberposts\' => -1));
    $total_posts = count($all_posts);
    $posts_per_column = intval($total_posts / 3);
    $count = 0;
    $col = 1;
    foreach($all_posts as $post):
  ?>

  <!--post-->
  <div class=\'post\' id=\'article<?php echo $count; ?>\'>
    <div class=\'thumb\'> 
      <a href=\'<?php the_permalink() ?>\'>
      <!--todo: round thumb images and make them of variable height-->
      <?php the_post_thumbnail() ?>
      <?php 
        $cat = get_the_category(); 
        $cat_long = $cat[0]->cat_name;
        $cat_split = explode(\' \', $cat_long); 
        $cat_short = strtolower($cat_split[0]);
      ?>
      </a>
      <span class=\'tag <?php echo $cat_short ?>\'><?php echo $cat_long ?></span>
    </div>

    <div class=\'inner <?php echo $cat_short ?>\'>
      <h2 class=\'entry-title\'><a href=\'<?php the_permalink() ?>\'><?php the_title() ?></a></h2>
    </div>
    <div class=\'entry-content\'>
      <?php the_excerpt() ?>
    </div>
  </div>
  <!--end post-->

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您需要使用setup_postdata

foreach($all_posts as $post):
    setup_postdata($post);
    // the rest of your loop

If you look at the docs for get_posts you will see that in all of the examples.

SO网友:ceruleus

更改此项:

<?php 
    $all_posts = get_posts(array(\'numberposts\' => -1));
    $total_posts = count($all_posts);
    $posts_per_column = intval($total_posts / 3);
    $count = 0;
    $col = 1;
    foreach($all_posts as $post):
  ?>
对此:

<?php
    $all_posts = new WP_Query(array(\'posts_per_page\' => -1));
    $total_posts = $all_posts->found_posts;
    $posts_per_column = intval($total_posts / 3);
    $count = 0;
    $col = 1;

    if($all_posts->have_posts()):
        while($all_posts->have_posts()):
            $all_posts->the_post();
?>
最后,你有一个终点。将其移除并放置:

<?php
        endwhile;
    endif;

    wp_reset_postdata();
?>
这是正确的方法(在我看来),因此您不能像在您的案例中那样使用$post变量来访问bug。在您的情况下,看起来您刚刚覆盖了全局$post变量-我想知道您的一些“the\\”标记是如何工作的,而一些没有。

结束

相关推荐

带有短码转义的_excerpt()或get_the_excerpt

我有几篇帖子在第一个字符周围有dropcap快捷码,如下所示:[dropcap]T[/dropcap]his is a dropcap. 替换此短代码将产生:<span class=\"dropcap\">T</span>his is a dropcap. 但是,当我在这些帖子上调用\\u摘录()或获取\\u摘录()时,它返回:\"his is a dropcap\". 我需要它返回“这是一个dropcap”,就像不存在任何短代码一样。