当前位置:网站首页 > WordPress教程 > 正文
如何在WordPress中显示最新的粘帖
作者:98wpeu发布时间:2024-01-20分类:WordPress教程浏览:376
导读:WordPress有一个非常酷的功能,叫做粘帖。将粘性帖子视为博客的特色帖子.当你将一篇帖子标记为粘性时,它会显示在你的新帖子上方,但前提是你的主题允许.在本教程中,我们将向您展示...
WordPress有一个非常酷的功能,叫做粘帖。将粘性帖子视为博客的特色帖子.当你将一篇帖子标记为粘性时,它会显示在你的新帖子上方,但前提是你的主题允许.在本教程中,我们将向您展示如何在WordPress中显示最新的粘性帖子。

WordPress注意:这是一个中级教程,需要基本的知识+WordPress主题知识.
视频教程
订阅塔克网如果您不喜欢视频或需要更多说明,请继续阅读.
您需要做的第一件事是将此代码片段复制并粘贴到主题的函数.php文件或站点特定的插件中。
function wpb_latest_sticky() {
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
// The Loop
if ( $the_query->have_posts() ) {
$return .= '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$return .= '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';
}
$return .= '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
return $return;
}
add_shortcode('latest_stickies', 'wpb_latest_sticky');
❤️由托管WPCode在WordPress中一键使用
上面的代码查询wordPress数据库以检索5个最新的粘性帖子。然后,它以列表形式显示每个粘性帖子的标题和一个链接.我们已经将所有这些都包装在一个函数中,并创建了一个短代码.
最新WordPress现在,要显示你最新的粘性帖子,你可以在任何[帖子、页面,甚至文本小工具中使用短码_Stickies]
如果您想在文本小部件中使用快捷码,那么您需要在主题的函数.php或特定于站点的插件中添加这行额外的代码。
add_filter('widget_text', 'do_shortcode');
❤️由托管WPCode在WordPress中一键使用
这个片段和函数可以很好地用在特色滑块中,或者你想要在你的网站上显示的任何其他高级功能中.这段代码主要针对的是具有定制主页或杂志风格外观的WordPress站点。
仅此而已,我们希望这篇文章能帮助你在你的WordPress博客上展示最新的粘性帖子。你可能也想看看我们的教程,关于如何在WordPress中为粘性帖子添加到期日期。
谷歌和+。
- WordPress教程排行
- 最近发表


