WordPress分类与标签等存档页实现置顶的方法

  介绍

这篇文章将为大家详细讲解有关WordPress分类与标签等存档页实现置顶的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

<强> WordPress分类与标签等存档页怎么实现置顶?

本文实例讲述了WordPress分类与标签等存档页实现置顶的方法。分享给大家供大家参考。具体分析如下:

在WordPress中默认能置顶文章就是只有首页了,如果我们希望分类/标签等存档页也能置顶文章我们需要二次开发。

现在参考wp-includes/query.php中首页置顶的代码,稍微修改一下,可以让分类页,标签页,作者页和日期页等存档页面也能像首页一样在顶部显示其范围内的置顶文章,把下面的代码放到当前主题下的中显然也就可以了。

代码如下:

add_filter (& # 39; the_posts& # 39; & # 39; putStickyOnTop& # 39;);   函数putStickyOnTop(美元的帖子){   如果(is_home () | | ! is_main_query () | | ! is_archive ())   返回文章美元;   全球wp_query美元;   $ sticky_posts=get_option (& # 39; sticky_posts& # 39;);   如果($ wp_query→query_vars[& # 39;分页# 39;]& lt;=1,,is_array (sticky_posts美元),,! emptyempty (sticky_posts美元),,! get_query_var (& # 39; ignore_sticky_posts& # 39;)) {$ stickies1=get_posts(数组(& # 39;post__in& # 39;=比;sticky_posts美元));   foreach (stickies1 sticky_post1美元美元){//判断当前是否分类页   如果($ wp_query→is_category==1,,美元! has_category (wp_query→query_vars[& # 39;猫# 39;],美元sticky_post1→ID)) {//去除不属于本分类的文章   $ offset1=函数(sticky_post1→美元ID, sticky_posts美元);   设置($ sticky_posts [$ offset1]);   }   如果($ wp_query→is_tag==1,,has_tag ($ wp_query→query_vars[& # 39;标签# 39;],美元sticky_post1→ID)) {//去除不属于本标签的文章   $ offset1=函数(sticky_post1→美元ID, sticky_posts美元);   设置($ sticky_posts [$ offset1]);   }   如果($ wp_query→is_year==1,,date_i18n (& # 39; y # 39;, strtotime(美元sticky_post1→post_date)) !=$ wp_query→查询(& # 39;猴子# 39;)){//去除不属于本年份的文章   $ offset1=函数(sticky_post1→美元ID, sticky_posts美元);   设置($ sticky_posts [$ offset1]);   }   如果($ wp_query→is_month==1,,date_i18n (& # 39; Ym& # 39;, strtotime(美元sticky_post1→post_date)) !=$ wp_query→查询(& # 39;猴子# 39;)){//去除不属于本月份的文章   $ offset1=函数(sticky_post1→美元ID, sticky_posts美元);   设置($ sticky_posts [$ offset1]);   }   如果($ wp_query→is_day==1,,date_i18n (& # 39; ymd # 39;, strtotime(美元sticky_post1→post_date)) !=$ wp_query→查询(& # 39;猴子# 39;)){//去除不属于本日期的文章   $ offset1=函数(sticky_post1→美元ID, sticky_posts美元);   设置($ sticky_posts [$ offset1]);   }   如果($ wp_query→is_author==1,,美元sticky_post1→post_author !=$ wp_query→query_vars[& # 39;作者# 39;)){//去除不属于本作者的文章   $ offset1=函数(sticky_post1→美元ID, sticky_posts美元);   设置($ sticky_posts [$ offset1]);   }   }   $ num_posts=count($职位);   美元sticky_offset=0;//遍历文章和便条纸搬迁到前面。   ($ i=0;美元我& lt;num_posts美元;$我+ +){   如果in_array(帖子美元($ i)→ID, sticky_posts美元)){   sticky_post=帖子美元($ i);//删除从当前位置粘   作用(帖子,我美元,1);//移动前,后其他的便条纸   作用是文章,sticky_offset美元0阵列(sticky_post美元));//增加粘性的偏移量。接下来的粘性将被放置在这个偏移量。   美元sticky_offset + +;//从粘性的帖子删除post数组   抵消美元=函数(sticky_post→美元ID, sticky_posts美元);   设置(sticky_posts美元(美元抵消));   }   }//如果有专门文章被排除,忽略那些黏糊糊的。   如果(! emptyempty (sticky_posts美元),,美元! emptyempty (wp_query→query_vars [& # 39; post__not_in& # 39;)))   美元sticky_posts=array_diff (sticky_posts, wp_query→美元query_vars [& # 39; post__not_in& # 39;]);//获取粘性的帖子,不是# 39;t的查询结果   如果(! emptyempty (sticky_posts美元)){   便条纸美元=get_posts(阵列(   & # 39;post__in& # 39;=比;sticky_posts美元,   & # 39;post_type& # 39;=比;美元wp_query→query_vars [& # 39; post_type& # 39;],   & # 39;post_status& # 39;=比;& # 39;发布# 39;   & # 39;nopaging& # 39;=比;真正的   ));   foreach(便条纸作为sticky_post美元){   作用是文章,sticky_offset美元0阵列(sticky_post美元));   美元sticky_offset + +;   }   }   }   返回文章美元;   }

WordPress分类与标签等存档页实现置顶的方法