企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
一个置顶帖子的帖子将被放置在帖子顶部的顶部。 此功能仅适用于内置的帖子类型的帖子,而不适用于自定义的帖子类型。 ## 如何置顶帖子 转到管理屏幕>帖子>添加新的或编辑 在右侧菜单中,单击发布组中的可见性编辑链接 点击粘贴此帖子到首页选项 ##显示置顶的帖子 只显示第一个置顶的帖子。 至少有一个帖子必须指定为“置顶帖子”,否则循环将显示所有帖子: ``` $sticky = get_option( 'sticky_posts' ); $query = new WP_Query( 'p=' . $sticky[0] ); ``` 显示第一个置顶的帖子,如果没有返回最后发布的帖子: ``` $args = array( 'posts_per_page' => 1, 'post__in' => get_option( 'sticky_posts' ), 'ignore_sticky_posts' => 1 ); $query = new WP_Query( $args ); ``` 只显示第一个置顶帖子,如果没有返回任何内容: ``` $sticky = get_option( 'sticky_posts' ); $args = array( 'posts_per_page' => 1, 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ); $query = new WP_Query( $args ); if ( isset( $sticky[0] ) ) { // insert here your stuff... } ``` ## 不显示置顶帖子 从查询中排除所有置顶的帖子: ``` $query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) ); ``` 排除类别中的置顶帖子。 返回类别中的所有帖子,但不要在顶部显示置顶帖子。 “置顶”仍将显示在自然的位置(例如按日期): ``` $query = new WP_Query( 'ignore_sticky_posts=1&posts_per_page=3&cat=6' ); ``` 排除类别中的置顶帖子。 返回类别中的帖子,但完全置顶粘贴帖子,并遵守分页规则: ``` $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $sticky = get_option( 'sticky_posts' ); $args = array( 'cat' => 3, 'ignore_sticky_posts' => 1, 'post__not_in' => $sticky, 'paged' => $paged ); $query = new WP_Query( $args ); ``` >[info] 注意:如果希望此查询在您设置为静态首页的页面模板中工作,请使用get_query_var('page')。 ``` <?php /* Get all Sticky Posts */ $sticky = get_option( 'sticky_posts' ); /* Sort Sticky Posts, newest at the top */ rsort( $sticky ); /* Get top 5 Sticky Posts */ $sticky = array_slice( $sticky, 0, 5 ); /* Query Sticky Posts */ $query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) ); ?> ``` ## 风格置顶帖子 为了帮助主题作者执行更简单的样式,post_class()函数用于将class =“...”添加到DIV,只需添加: ``` <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> ``` post_class()输出该div的`class="whatever"`片段。 这包括几个不同的价值类别:post,hentry(对于hAtom微格式页面),category-X(其中X是帖子的每个类别的块),以及tag-X(类似的,但带有标签)。 它还为标记为粘滞帖子的帖子添加了“粘性”。 ``` .sticky { color:red; } ``` >[info] 注意:只有在主页的第一页(is_home()为true并且is_paged()为false)的置顶帖子中才添加“sticky”类。