### [WordPress分类与标签等存档页怎么实现置顶](https://www.prosaas.cn/article/9311) **Published:** 2019-12-28T01:52:26 **Author:** 5201314 **Excerpt:** WordPress分类与标签等存档页怎么实现置顶?本文实例讲述了WordPress分类与标签等存档页实现置顶的… **![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212132229106.jpg)** **WordPress分类与标签等存档页怎么实现置顶?** 本文实例讲述了WordPress分类与标签等存档页实现置顶的方法。分享给大家供大家参考。具体分析如下: 推荐:《**wordpress教程**》 在wordpress中默认能置顶文章就是只有首页了,如果我们希望分类/标签等存档页也能置顶文章我们需要二次开发. 现在参考wp-includes/query.php中首页置顶的代码,稍微修改一下,可以让分类页、标签页、作者页和日期页等存档页面也能像首页一样在顶部显示其范围内的置顶文章,把下面的代码放到当前主题下的functions.php中就可以了. 代码如下: 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 && is\_array($sticky\_posts) && !emptyempty($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 ( !emptyempty($sticky\_posts) && !emptyempty($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 ( !emptyempty($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; } 代码说明: 1、如果你想让存档页也都显示全部置顶文章,那么就删掉11-43行的代码; 2、如果不想在某分类页显示置顶文章,将第 3 行的 代码如下: if( //改成: // abc是分类名称 if ( is\_category( 'abc' ) || 3、如果不想某标签页显示置顶文章,将第 3 行的代码 代码如下: if( //改成: // abc是标签名称 if ( is\_tag( 'abc' ) || 4、如果不想某作者页显示置顶文章,将第 3 行的 代码如下: if( //改成: // abc是作者昵称 if ( is\_author( 'abc' ) || 5、以上代码只对主循环有效,如果你在存档页使用WP\_Query或query\_posts来获取文章列表,又像让这些列表顶部显示置顶文章,可以把第3行代码中的以下代码删掉(注意:可能会导致文章显示数量跟你设置的不一样): 代码如下: 代码如下: || !is\_main\_query() 置顶样式:如果你想给置顶文章添加样式,将以下代码添加到functions.php中,会给置顶文章添加一个名为 sticky 的class,具体的css代码,再自行自定义: 代码如下: add\_filter('post\_class', 'addStickyClass' ,10,3 ); function addStickyClass( $classes, $class, $post\_id ){ if( is\_sticky() && is\_category() && !isset( $classes\['sticky'\] ) ){ $classes\[\] = 'sticky'; } return $classes; } 希望本文所述对大家的**WordPress建站**有所帮助。 以上就是WordPress分类与标签等存档页怎么实现置顶的详细内容,更多请关注知企PROSAAS其它相关文章! **Tags:** class, css, php, WordPress, 循环 **Categories:** WordPress ---