久久久久久久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實現無限極分類的具體思路、實現代碼與相關注意事項,需要的朋友可以參考下
主站蜘蛛池模板: 国产激情视频在线 | 96国产精品久久久久aⅴ四区 | 日日精品 | 中文字幕在线第一页 | 亚洲视频在线播放 | 成人国产在线观看 | 中国黄色在线视频 | 天堂在线1| 久久久999精品 | 一区二区三区精品在线 | 欧美精品91 | 免费av观看 | 精品欧美一区二区三区久久久 | 第一区在线观看免费国语入口 | 91精品久久久久久久久中文字幕 | 午夜视频在线观看一区二区 | 能免费看的av | 日韩精品a在线观看图片 | 国产精品123区 | 日日夜夜精品视频 | 国产午夜精品久久久久免费视高清 | 羞羞视频免费观 | 国产精品久久久久久久久免费桃花 | 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 在线视频成人 | 久草在线视频中文 | 99久久久久久99国产精品免 | 九色网址 | 免费在线精品视频 | 久久久国产一区二区三区 | 成人在线网 | 一区二区三区四区免费视频 | 中日字幕大片在线播放 | 国产午夜亚洲精品不卡 | 成人在线 | 国产区在线视频 | 久久com| 一级片在线观看 | 亚洲九九 | 欲色av | 久久久久久久久久久91 |