WordPress文章摘录没有显示在我最近的文章小工具中


WordPress Post Excerpt is not showing in my Recent Post Widget

我正在使用get_posts从数据库中检索posts信息。它返回"文章标题"、"缩略图"、"文章类别"answers"文章摘录"。一切都很好,但问题是我无法显示帖子摘录。

这是我的代码:

function widget ($args,$instance) {
   extract($args);
  $title = $instance['title'];
  $catid = $instance['catid'];
  $numberposts = $instance['numberposts'];
  $date = $instance['date'];
  $rss = $instance['rss'];
  // retrieve posts information from database
  global $wpdb;
  $posts = get_posts('post_type=post&numberposts='.$numberposts.'&category='.$catid);
  $out = '<ul>';
  if ($posts) {
      foreach($posts as $post) { 
      setup_postdata($post);
      $out .= '<li>'.get_the_post_thumbnail($post->ID,'medium').'</li>';
      $out .= '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';    
      $out .= '<li>'.$post->post_excerpt.'</li>';
      if ($date) $out .= '<li>'.date('d/m/Y', strtotime($post->post_date_gmt)).'</li>';
      }
  } 
  if ($rss) $out .= '<li><a href="'.get_category_link($catid).'feed/" class="rss">Category RSS</a></li>';
  $out .= '</ul>';
  //print the widget for the sidebar
  echo $before_widget;
  echo $before_title.$title.$after_title;
  echo $out;
  echo $after_widget;
 }
}

$post->post_excerpt并不像您想象的那样工作。大多数人认为这与模板标签the_excerpt()相同,并且它不是

CCD_ 3是通过截断CCD_。CCD_ 5根本不生成,因为这是用户定义的。此摘录是用户在摘录元框中的后期编辑屏幕中手动添加的摘录文本。(默认情况下,此元框是隐藏的,但可以在屏幕顶部的"屏幕选项"选项卡中启用)。如果用户没有指定手动摘录,$post->post_excerpt将不返回任何内容,这就是为什么您会看到这种行为

您已经设置了postdata,所以您可以直接使用模板标签,因此可以使用the_excerpt() 代替$post->post_excerpt

编辑

感谢下面的评论,我没有考虑到摘录不应该立即被回音。在这种情况下,您将使用get_the_excerpt(),它不回显文本,而是简单地检索文本。