我需要从News
分类并在侧栏中显示它们。我使用下面的代码
<?php $news_id = get_cat_ID(\'News\');
if($news_id) {
echo \'<div class="entry-title"><a href="\'.get_category_link( $news_id ).\'">\'.get_the_category_by_ID( $news_id ).\'</a></div>\';
$recent_news = wp_get_recent_posts( array(\'numberposts\' => 2, \'category\' => $news_id, \'post_status\' => \'publish\'));
if ($recent_news) {
echo \'<ul class="sidebar-posts-list">\';
foreach ($recent_news as $news_item) {
echo \'<li><div class="item-title">\' . $news_item["post_title"].\'</div>\';
echo \'<div class="item-text">\' . wp_trim_words( $news_item["post_content"], 20 ) . \'</div>\';
echo \'<a href="\' . get_permalink($news_item["ID"]) . \'" class="read-more">Read more</a></li>\';
}
echo \'</ul>\';
}
}?>
但我不确定
wp_trim_words( $news_item["post_content"], 20 )
是得到我想要的东西的正确方式。这是可行的,但我不确定它是否正确。也许有更好的方法?