如何在WordPress中显示最新置顶帖子

   2025-07-18 admin00100

wordpress有一个非常酷的功能,称为粘帖。将置顶帖子视为博客的特色帖子。当您将帖子标记为置顶时,它会显示在您的新帖子上方,但前提是您的主题允许。在本教程中,我们将向您展示如何在wordpress中显示最新的置顶帖子。

注意:这是一个中级教程,需要基本的HTML/CSS知识+WordPress主题知识。

视频教程

https://www.youtube.com/embed/qI3S-tNKWng?version=3&rel=0&fs=1&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent订阅WPBeginner

https://www.youtube.com/subscribe_embed?usegapi=1&channel=wpbeginner&layout=default&count=default&origin=https%3A%2F%2Fwpbeginner.com&gsrc=https://www.yaozhongpeng.com/static/image/nopic320.png>

如果您不喜欢该视频或需要更多说明,请继续阅读。

您需要做的第一件事是复制此代码片段并将其粘贴到主题的functions.php文件或特定于站点的插件中。

functionwpb_latest_sticky(){$sticky=get_option('sticky_posts');rsort($sticky);$sticky=array_slice($sticky,0,5);$the_query=newWP_Query(array('post__in'=>$sticky,'ignore_sticky_posts'=>1));//TheLoopif($the_query->have_posts()){$return.='<ul>';while($the_query->have_posts()){$the_query->the_post();$return.='<li><ahref="'.get_permalink().'"title="'.get_the_title().'">'.get_the_title().'</a><br/>'.get_the_excerpt().'</li>';}$return.='</ul>';}else{//nopostsfound}wp_reset_postdata();return$return;}add_shortcode('latest_stickies','wpb_latest_sticky');
 
举报收藏 0打赏 0评论 0
 
更多>同类资讯
推荐图文
推荐资讯
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  版权声明  |  网站地图  |  RSS订阅
Powered By DESTOON