机场测评与节点★ 4.8 推荐评级

三种WordPress分类目录页文章置顶的方法_插件_代码

博主头像站长评测
发布于 2019/11/18(修订于 2019/11/27)原创独立站体系

WordPress文章置顶显示想必你知道怎么实现,只需要在发表文章的时候勾选在博客中置顶( 将文章置于首页顶端)的选项就可以了。

但是如何设置分类页面的置顶文章,只在WP分类页显示置顶文章不在首页显示呢?本文给大家介绍两款WordPress分类页文章置顶的插件和代码实现方法。

分类目录文章置顶插件

这里介绍的可以实现文章在分类页置顶的插件有两款,VPS机场百科在WordPrss 5.3版本下测试正常工作。

Category Sticky Post

推荐使用Category Sticky Post这款WordPress插件,可以很轻松的实现文章分类页置顶的效果。

Category Sticky Pos允许您在每个分类目录页面置顶显示指定文章内容。

  • 允许您选择要置顶显示文章的分类目录
  • 就像内置的置顶功能一样,在分类目录置顶显示文章
  • 允许在特定分类目录显示不同的置顶文章
  • 支持WordPress 5.3

安装好插件后,发表文章的时候在右侧边栏会出现Category Sticky的选项,选择你要置顶的分类就可以了。

下载地址

Sticky Posts – Switch

Sticky Posts – Switch插件新一些,支持在分类目录显示置顶文章外,还支持标签页和自定义帖子类型上显示。

而且通过该插件置顶的文章在后台文章列表里面会显示一个星星图标,方便你管理置顶文章。

同时,可以在设置里面显示是否在首页展示置顶文章。

下载地址

代码实现分类显示置顶文章

代码版笔者没有测试,不想使用插件的可以自己研究一下代码实现方法。

在你主题函数文件functions.php里面插入下面的代码,如果不知道怎么插入,可以使用插件帮忙,参考:安全添加代码到functions.php文件的方法:Code Snippets

add_filter(‘the_posts’, ‘putStickyOnTop’ ); function putStickyOnTop( $posts ) { if(is_home() || !is_main_query() || !is_archive()) return $posts;

global $wp_query;

// 获取所有置顶文章 $sticky_posts = get_option(‘sticky_posts’);

if ( $wp_query->query_vars[‘paged’] <= 1 && !empty($sticky_posts) && is_array($sticky_posts) && !get_query_var(‘ignore_sticky_posts’) ) { $stickies1 = get_posts( array( ‘post__in’ => $sticky_posts ) ); foreach ( $stickies1 as $sticky_post1 ) { // 判断当前是否分类页 if($wp_query->is_category == 1 && !has_category($wp_query->query_vars[‘cat’], $sticky_post1->ID)) { // 去除不属于本分类的置顶文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_tag == 1 && !has_tag($wp_query->query_vars[‘tag’], $sticky_post1->ID)) { // 去除不属于本标签的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_year == 1 && date_i18n(‘Y’, strtotime($sticky_post1->post_date))!=$wp_query->query[‘m’]) { // 去除不属于本年份的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_month == 1 && date_i18n(‘Ym’, strtotime($sticky_post1->post_date))!=$wp_query->query[‘m’]) { // 去除不属于本月份的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_day == 1 && date_i18n(‘Ymd’, strtotime($sticky_post1->post_date))!=$wp_query->query[‘m’]) { // 去除不属于本日期的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_author == 1 && $sticky_post1->post_author != $wp_query->query_vars[‘author’]) { // 去除不属于本作者的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } }

$num\_posts \= count($posts);
$sticky\_offset \= 0;
// Loop over posts and relocate stickies to the front.
for ( $i \= 0; $i < $num\_posts; $i++ ) {
  if ( in\_array($posts\[$i\]\-\>ID, $sticky\_posts) ) {
    $sticky\_post \= $posts\[$i\];
    // Remove sticky from current position
    array\_splice($posts, $i, 1);
    // Move to front, after other stickies
    array\_splice($posts, $sticky\_offset, 0, array($sticky\_post));
    // Increment the sticky offset. The next sticky will be placed at this offset.
    $sticky\_offset++;
    // Remove post from sticky posts array
    $offset \= array\_search($sticky\_post\-\>ID, $sticky\_posts);
    unset( $sticky\_posts\[$offset\] );
  }
}

// If any posts have been excluded specifically, Ignore those that are sticky.
if ( !empty($sticky\_posts) && !empty($wp\_query\-\>query\_vars\['post\_\_not\_in'\] ) )
  $sticky\_posts \= array\_diff($sticky\_posts, $wp\_query\-\>query\_vars\['post\_\_not\_in'\]);

// Fetch sticky posts that weren't in the query results
if ( !empty($sticky\_posts) ) {
  $stickies \= get\_posts( array(
    'post\_\_in' \=\> $sticky\_posts,
    'post\_type' \=\> $wp\_query\-\>query\_vars\['post\_type'\],
    'post\_status' \=\> 'publish',
    'nopaging' \=\> true
  ) );

  foreach ( $stickies as $sticky\_post ) {
    array\_splice( $posts, $sticky\_offset, 0, array( $sticky\_post ) );
    $sticky\_offset++;
  }
}

}

return $posts; }

代码说明

①、如果你想让存档页也显示全部置顶文章,那么就删掉12-45行的代码;
②、如果不想在某分类页显示置顶文章,将第 3 行的if(修改为

// abc是分类名称 if ( is_category( ‘abc’ ) ||

③、如果不想某标签页显示置顶文章,将第 3 行的if(修改为:

// abc是标签名称 if ( is_tag( ‘abc’ ) ||

④、如果不想某作者页显示置顶文章,将第 3 行的if(修改为:

// abc是作者昵称 if ( is_author( ‘abc’ ) ||

⑤、以上代码只对主循环有效,如果你在存档页使用WP_Query或query_posts来获取文章列表,又想让这些列表顶部显示置顶文章,可以把第3行代码中的以下代码删掉(注意:可能会导致文章显示数量跟你设置的不一样):

|| !is_main_query()

代码方式来自露兜博客