简体   繁体   English

WordPress帖子摘录未显示在我的“最近发布”小部件中

[英]WordPress Post Excerpt is not showing in my Recent Post Widget

I am using get_posts to retrieve posts information from database. 我正在使用get_posts从数据库检索帖子信息。 It returns "Post title", "thumbnail", "post category", and "post excerpt". 它返回“帖子标题”,“缩略图”,“帖子类别”和“帖子摘录”。 Everything is working fine but the problem is I am unable to show post excerpt. 一切正常,但问题是我无法显示摘录。

Here is my code: 这是我的代码:

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 does not get work the way you think it does. $post->post_excerpt无法以您认为的方式工作。 Most people think this is the same as the template tag the_excerpt() , and it is not 大多数人认为这与模板标签the_excerpt() ,但与

the_excerpt() is generated by truncating get_the_content() . the_excerpt()是通过截断get_the_content()生成的。 $post->post_excerpt is not generated at all as this is user defined. $post->post_excerpt完全不会生成,因为这是用户定义的。 This excerpt is the excerpt text manually added by the user in the post edit screen in the excerpt meta box. 该摘录是用户在摘录元框中的帖子编辑屏幕中手动添加的摘录文本。 (This meta box is hidden by default, but can be enabled in the "Screen Options" tab on the top part of the screen). (此元框默认情况下是隐藏的,但可以在屏幕顶部的“屏幕选项”选项卡中启用)。 If the user did not specify a manual excerpt, $post->post_excerpt will return nothing, that is why you see this behavior 如果用户未指定手动摘录,则$post->post_excerpt将不返回任何内容,这就是您看到此行为的原因

You have already set up your postdata, so you can just simply use the template tags directly, so in place of $post->post_excerpt , you can use the_excerpt() 您已经设置了postdata,因此可以直接使用模板标签,因此可以使用the_excerpt()代替$post->post_excerpt

EDIT 编辑

Thanks to the comment below, I did not take into account that the excerpt should not be echo'd straight away. 由于下面的评论,我没有考虑到摘录不应立即回显。 In this case, you would make use of get_the_excerpt() which does not echo the text, but simply retrieves it. 在这种情况下,您将使用get_the_excerpt() ,它不回显文本,而只是检索文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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