首頁 CMS教程 &#&按 WordPress分類與標籤等存檔頁怎麼實現置頂

WordPress分類與標籤等存檔頁怎麼實現置頂

Dec 28, 2019 am 09:52 AM
wordpress

WordPress分類與標籤等存檔頁怎麼實現置頂

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[&#39;paged&#39;] <= 1 && is_array($sticky_posts) && !emptyempty($sticky_posts) && !get_query_var(&#39;ignore_sticky_posts&#39;) ) { $stickies1 = get_posts( array( &#39;post__in&#39; => $sticky_posts ) ); 
foreach ( $stickies1 as $sticky_post1 ) { 
// 判断当前是否分类页 
if($wp_query->is_category == 1 && !has_category($wp_query->query_vars[&#39;cat&#39;], $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[&#39;tag&#39;], $sticky_post1->ID)) { 
// 去除不属于本标签的文章 
$offset1 = array_search($sticky_post1->ID, $sticky_posts); 
unset( $sticky_posts[$offset1] ); 
} 
if($wp_query->is_year == 1 && date_i18n(&#39;Y&#39;, strtotime($sticky_post1->post_date))!=$wp_query->query[&#39;m&#39;]) { 
// 去除不属于本年份的文章 
$offset1 = array_search($sticky_post1->ID, $sticky_posts); 
unset( $sticky_posts[$offset1] ); 
} 
if($wp_query->is_month == 1 && date_i18n(&#39;Ym&#39;, strtotime($sticky_post1->post_date))!=$wp_query->query[&#39;m&#39;]) { 
// 去除不属于本月份的文章 
$offset1 = array_search($sticky_post1->ID, $sticky_posts); 
unset( $sticky_posts[$offset1] ); 
} 
if($wp_query->is_day == 1 && date_i18n(&#39;Ymd&#39;, strtotime($sticky_post1->post_date))!=$wp_query->query[&#39;m&#39;]) { 
// 去除不属于本日期的文章 
$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[&#39;author&#39;]) { 
// 去除不属于本作者的文章 
$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[&#39;post__not_in&#39;] ) ) 
$sticky_posts = array_diff($sticky_posts, $wp_query->query_vars[&#39;post__not_in&#39;]); 
// Fetch sticky posts that weren&#39;t in the query results 
if ( !emptyempty($sticky_posts) ) { 
$stickies = get_posts( array( 
&#39;post__in&#39; => $sticky_posts, 
&#39;post_type&#39; => $wp_query->query_vars[&#39;post_type&#39;], 
&#39;post_status&#39; => &#39;publish&#39;, 
&#39;nopaging&#39; => 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( &#39;abc&#39; ) ||
登入後複製

3、如果不想某標籤頁顯示置頂文章,將第3 行的代碼

程式碼如下:

if( 
//改成: 
// abc是标签名称 
if ( is_tag( &#39;abc&#39; ) ||
登入後複製

4、如果不想某作者頁顯示置頂文章,將第3 行的

程式碼如下:

if( 
//改成: 
// abc是作者昵称 
if ( is_author( &#39;abc&#39; ) ||
登入後複製

5、以上程式碼只對主循環有效,如果你在存檔頁使用WP_Query或query_posts來獲取文章列表,又像讓這些列表頂部顯示置頂文章,可以把第3行代碼中的以下代碼刪掉(注意:可能會導致文章顯示數量跟你設定的不一樣):

程式碼如下:

程式碼如下:

|| !is_main_query()
登入後複製

置頂樣式:如果你想為置頂文章新增樣式,將以下程式碼加入functions .php中,會為置頂文章加上一個名為sticky 的class,具體的css程式碼,再自行自訂:

程式碼如下:

add_filter(&#39;post_class&#39;, &#39;addStickyClass&#39; ,10,3 ); 
function addStickyClass( $classes, $class, $post_id ){ 
if( is_sticky() && is_category() && !isset( $classes[&#39;sticky&#39;] ) ){ 
$classes[] = &#39;sticky&#39;; 
} 
return $classes; 
}
登入後複製

希望本文所述對大家的WordPress建站有幫助。

以上是WordPress分類與標籤等存檔頁怎麼實現置頂的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

wordpress文章列表怎麼調 wordpress文章列表怎麼調 Apr 20, 2025 am 10:48 AM

有四種方法可以調整 WordPress 文章列表:使用主題選項、使用插件(如 Post Types Order、WP Post List、Boxy Stuff)、使用代碼(在 functions.php 文件中添加設置)或直接修改 WordPress 數據庫。

wordpress屏蔽ip的插件有哪些 wordpress屏蔽ip的插件有哪些 Apr 20, 2025 am 08:27 AM

WordPress 屏蔽 IP 的插件選擇至關重要。可考慮以下類型:基於 .htaccess:高效,但操作複雜;數據庫操作:靈活,但效率較低;基於防火牆:安全性能高,但配置複雜;自行編寫:最高控制權,但需要更多技術水平。

wordpress主機怎麼建站 wordpress主機怎麼建站 Apr 20, 2025 am 11:12 AM

要使用 WordPress 主機建站,需要:選擇一個可靠的主機提供商。購買一個域名。設置 WordPress 主機帳戶。選擇一個主題。添加頁面和文章。安裝插件。自定義您的網站。發布您的網站。

wordpress編輯日期怎麼取消 wordpress編輯日期怎麼取消 Apr 20, 2025 am 10:54 AM

WordPress 編輯日期可以通過三種方法取消:1. 安裝 Enable Post Date Disable 插件;2. 在 functions.php 文件中添加代碼;3. 手動編輯 wp_posts 表中的 post_modified 列。

wordpress主題頭部圖片如何更換 wordpress主題頭部圖片如何更換 Apr 20, 2025 am 10:00 AM

更換 WordPress 主題頭部圖片的分步指南:登錄 WordPress 儀錶盤,導航至“外觀”&gt;“主題”。選擇要編輯的主題,然後單擊“自定義”。打開“主題選項”面板並尋找“網站標頭”或“頭部圖片”選項。單擊“選擇圖像”按鈕並上傳新的頭部圖片。裁剪圖像並單擊“保存並裁剪”。單擊“保存並發布”按鈕以更新更改。

wordpress怎麼寫頁頭 wordpress怎麼寫頁頭 Apr 20, 2025 pm 12:09 PM

在WordPress中創建自定義頁頭的步驟如下:編輯主題文件“header.php”。添加您的網站名稱和描述。創建導航菜單。添加搜索欄。保存更改並查看您的自定義頁頭。

wordpress出現錯誤怎麼辦 wordpress出現錯誤怎麼辦 Apr 20, 2025 am 11:57 AM

WordPress 錯誤解決指南:500 內部服務器錯誤:禁用插件或檢查服務器錯誤日誌。 404 未找到頁面:檢查 permalink 並確保頁面鏈接正確。白屏死機:增加服務器 PHP 內存限制。數據庫連接錯誤:檢查數據庫服務器狀態和 WordPress 配置。其他技巧:啟用調試模式、檢查錯誤日誌和尋求支持。預防錯誤:定期更新 WordPress、僅安裝必要插件、定期備份網站和優化網站性能。

wordpress評論怎麼顯示 wordpress評論怎麼顯示 Apr 20, 2025 pm 12:06 PM

WordPress 網站中啟用評論功能:1. 登錄管理面板,轉到 "設置"-"討論",勾選 "允許評論";2. 選擇顯示評論的位置;3. 自定義評論表單;4. 管理評論,批准、拒絕或刪除;5. 使用 &lt;?php comments_template(); ?&gt; 標籤顯示評論;6. 啟用嵌套評論;7. 調整評論外形;8. 使用插件和驗證碼防止垃圾評論;9. 鼓勵用戶使用 Gravatar 頭像;10. 創建評論指

See all articles