Php WordPress帖子摘录未显示在我最近的帖子小部件中
我使用get_posts从数据库中检索posts信息。它返回“文章标题”、“缩略图”、“文章类别”和“文章摘要”。一切都很好,但问题是我无法显示文章摘要 这是我的密码:Php WordPress帖子摘录未显示在我最近的帖子小部件中,php,widget,wordpress,Php,Widget,Wordpress,我使用get_posts从数据库中检索posts信息。它返回“文章标题”、“缩略图”、“文章类别”和“文章摘要”。一切都很好,但问题是我无法显示文章摘要 这是我的密码: function widget ($args,$instance) { extract($args); $title = $instance['title']; $catid = $instance['catid']; $numberposts = $instance['numberposts']; $d
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;
}
}
函数小部件($args,$instance){
摘录($args);
$title=$instance['title'];
$catid=$instance['catid'];
$numberposts=$instance['numberposts'];
$date=$instance['date'];
$rss=$instance['rss'];
//从数据库检索帖子信息
全球$wpdb;
$posts=get_posts($post_type=post&numberposts='.$numberposts.&category='.$catid);
$out='';
如果有的话(员额){
foreach(员额为$员额){
设置_postdata($post);
$out.='- '。获取“post”缩略图($post->ID,'medium')。
;
$out.='';
$out.='- 。$post->post_摘录。
;
如果($date)$out.='- '.date('d/m/Y',strottime($post->post\u date\u gmt))。
';
}
}
如果($rss)$out.='';
$out.='
';
//打印侧边栏的小部件
echo$before_小部件;
回音$before_title.$title.$before_title;
回音$out;
echo$after_小部件;
}
}
$post->post\u摘录
并没有像你想象的那样得到工作。大多数人认为这与模板标签the_extract()
相同,但事实并非如此
通过截断获取内容()
生成摘录()<代码>$post->post_摘录
根本不生成,因为这是用户定义的。此摘录是用户在摘录元框的后期编辑屏幕中手动添加的摘录文本。(默认情况下,此元框是隐藏的,但可以在屏幕顶部的“屏幕选项”选项卡中启用)。如果用户没有指定手动摘录,$post->post\u extract
将不返回任何内容,这就是您看到此行为的原因
您已经设置了postdata,因此只需直接使用模板标记即可,因此您可以使用the_extract()
编辑
多亏了下面的评论,我没有考虑到摘录不应该马上被回音。在这种情况下,您可以使用
get_the_extract()
,它不回显文本,而只是检索文本。如果需要在回显之前构建格式化字符串(the_extract()
将立即回显),您可以使用,这将替代$post->post\u摘录
值。您是对的<代码>获取摘录()将是正确的选择。事实上,我还没有注意到这段摘录不应该马上被引用。谢谢你。