久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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行的代碼;

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了PHP實現無限極分類生成分類樹的方法,結合實例形式簡單分析了無限極分類的原理與實現方法,涉及PHP數組遍歷與判斷相關操作技巧,需要的朋友可以參考下
wordpress是很多新手站長搭建個人博客最喜愛的程序,但是最近在使用WordPress的時候遇到了一些問題,所以想著將遇到問題總結分享出來,下面這篇文章主要給大家介紹了關于wordpress在安
最近在工作中遇到一個需求,是要在laravel 5.4中實現無限級分類,但發現網上這個的資料較少,所以只能自己來實現了,下面這篇文章主要給大家介紹了關于在laravel 5.4中實現無限級分類
這篇文章主要介紹了PHP正則刪除html代碼中a標簽并保留標簽內容的方法,涉及php基于正則的字符串匹配與子表達式操作相關技巧,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了yii框架無限極分類的實現方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要介紹了PHP無限極分類函數的實現方法,結合實例形式詳細分析了php實現無限極分類的具體思路、實現代碼與相關注意事項,需要的朋友可以參考下
主站蜘蛛池模板: 给我免费的视频在线观看 | 国产精品日韩 | 日韩在线电影 | www国产成人免费观看视频,深夜成人网 | 伊人久久免费 | 亚洲系列第一页 | 日日日日操 | 日韩不卡一二区 | 国产91成人 | 龙珠z在线观看 | 91精品国产91久久久久游泳池 | 国产精品自拍视频网站 | 2019天天操| 精品视频一区二区三区在线观看 | 国产精品亚洲第一 | 国产精品久久久久久一级毛片 | 天天干视频网 | 精品国产第一区二区三区 | 国产精品久久片 | 成人区一区二区三区 | 国产亚洲精品精品国产亚洲综合 | 久久精品亚洲欧美日韩精品中文字幕 | 精品福利一区 | 一区二区三区精品视频 | 亚洲成人观看 | 永久网站 | 国产亚洲欧美在线视频 | 久草在线在线精品观看 | 视频一区二区中文字幕日韩 | 国产一区www | 日韩精品久久久久久 | 草久视频 | 精品久草 | 东京久久 | 欧美freesex黑人又粗又大 | 一区二区三区国产视频 | 超碰人人在线 | 亚洲欧美综合精品另类天天更新 | 黄色在线免费看 | 成人精品在线观看 | 久久久精品一区 |