WordPress分类与标签等存档页怎么实现置顶-WordPress

资源魔 62 0

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; 
}

代码阐明:

一、假如你想让存档页也都显示全副置顶文章,那末就删掉11-43行的代码;

二、假如没有想正在某分类页显示置顶文章,将第 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()

置顶款式:假如你想给置顶文章增加款式,将如下代码增加到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分类与标签等存档页怎样完成置顶的具体内容,更多请存眷资源魔其它相干文章!

标签: WordPress wordpress教程 wordpress自学 wordpress技术

抱歉,评论功能暂时关闭!