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行的程式碼;
2、如果不想在某分類頁顯示置頂文章,將第3 行的
代碼如下:
if( //改成: // abc是分类名称 if ( is_category( 'abc' ) ||
3、如果不想某標籤頁顯示置頂文章,將第3 行的代碼
程式碼如下:
if( //改成: // abc是标签名称 if ( is_tag( 'abc' ) ||
4、如果不想某作者頁顯示置頂文章,將第3 行的
程式碼如下:
if( //改成: // abc是作者昵称 if ( is_author( 'abc' ) ||
5、以上程式碼只對主循環有效,如果你在存檔頁使用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分類與標籤等存檔頁怎麼實現置頂的詳細內容。更多資訊請關注PHP中文網其他相關文章!

WordPressIsAcmsDuetoItseAsofuse,自定義,USERMANAMECTION,SEO和COMMUNITYSUPPORT.1)ITSIMPLIFIESCONTENTMANGAMEWITHANINTUISIDERFEEFFECE.2)提供extentensiveCustomizationThroughThroughTheMesandPlugins.3)supportrobustuserrolesandplugins.4)supportrobustuserrolesandpermissions.4)增強

在 WordPress 網站上啟用評論功能,可以為訪客提供參與討論和分享反饋的平台。為此,請按照以下步驟操作:啟用評論:在儀錶盤中,導航至“設置”>“討論”,並選中“允許評論”複選框。創建評論表單:在編輯器中,單擊“添加塊”並蒐索“評論”塊,將其添加到內容中。自定義評論表單:通過設置標題、標籤、佔位符和按鈕文本來定制評論塊。保存更改:單擊“更新”以保存評論框並將其添加到頁面或文章中。

如何復制 WordPress 子站?步驟:在主站創建子站。在主站克隆子站。將克隆導入目標位置。更新域名(可選)。分開插件和主題。

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

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

可以通過 WordPress 安裝 FTP 插件,配置 FTP 連接,然後使用文件管理器上傳源碼。步驟包括:安裝 FTP 插件、配置連接、瀏覽上傳位置、上傳文件、檢查上傳成功。

如何復制 WordPress 代碼?從管理界面複製:登錄 WordPress 網站,導航到目標位置,選擇代碼並按 Ctrl C (Windows)/Command C (Mac) 複製代碼。從文件複製:使用 SSH 或 FTP 連接到服務器,導航到主題或插件文件,選擇代碼並按 Ctrl C (Windows)/Command C (Mac) 複製代碼。

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中