GG資源網

WordPress建站教程:調整WordPress文章排序(WordPress分類與標籤等存檔頁實現置頂的方法)

WordPress建站教程:調整Wordpress文章排序

作者:悅然wordpress建站(悅然建站)

(此處已添加小程序,請到今日頭條客戶端查看)

繼續分享wordpress建站教程。今天給大家分享控制wordpress文章排序的方法。默認情況下wordpress網站的文章都是按時間排序的,這也比較符合文章更新的特點,一般情況下我們也沒有必要特別處理。

可是對企業網站建設來說,我們難免會有一些特殊的要求,比如一些特別的產品或新聞需要把它放在前面。接下來悅然企業網站建設就給大家分享幾個可以調整wordpress文章排序的方法。

1.時間控制

wordpress網站中默認情況是最新發布的文章是排在最前面的,所以我們可以通過修改時間的方法來控制文章排序。

比如你想讓一篇文章排前面,那就把這個文章的發布時間按當前最新直接直接發布即可,如果你想讓一篇文章排後台,那你只需要在發布文章時把發布時間調整靠後一些即可。

​如果是已經發布的文章,你可以在文章列表中快速編輯,修改文章的發布日期就可以了。

溫馨提示:通過修改時間來控制排序效果很好很直接,但是對網站SEO優化不利,所以這種方法盡量少用。

2.置頂功能

​你可以在文章編輯頁面直接選擇讓文章置頂,這樣即使以後有新的文章發布,這篇文章也會顯示在最前面。

​同樣你也可以給已發布的文章設置置頂,在文章列表中快速編輯,勾選置頂就可以了。

溫馨提示:wordpress網站自帶的置頂功能主要是針對博客網站中,但是很多企業網站因為都是二次開發過的,有些還取消也自帶的配置功能,所以有些wordpress企業網站可能配置功能不會生效。此時可以嘗試找第三方的置頂插件代替。

3.插件控制

前面的兩種調整文章排序的方法都不太靈活,最理想的狀態是我們可以像電腦桌面一下用滑鼠來手動文章的位置,而這樣的功能是可以實現的,只需要使用一個插件即可。

​給大家推薦的插件是post-types-order,這款插件可以直接在wordpress插件中心,或者是在wordpress官網下載,軟體可以免費使用,不過是英文的,但不影響使用。

​啟動插件之後你就可以直接手動文章排序了。

有些時候可能搜索不到post-types-order插件,我給大家提取了下載地址如下:

總結

關於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分類與標籤等存檔頁實現置頂的方法 (https://www.wpmee.com/) WordPress使用教程 第1張

由於網站搬家,部分鏈接失效,如無法下載,請聯繫站長!謝謝支持!
1. 帶 [親測] 說明源碼已經被站長親測過!
2. 下載後的源碼請在24小時內刪除,僅供學慣用途!
3. 分享目的僅供大家學習和交流,請不要用於商業用途!
4. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
5. 本站所有資源來源於站長上傳和網路,如有侵權請郵件聯繫站長!
6. 沒帶 [親測] 代表站長時間緊促,站長會保持每天更新 [親測] 源碼 !
7. 盜版ripro用戶購買ripro美化無擔保,若設置不成功/不生效我們不支持退款!
8. 本站提供的源碼、模板、插件等等其他資源,都不包含技術服務請大家諒解!
9. 如果你也有好源碼或者教程,可以到審核區發布,分享有金幣獎勵和額外收入!
10.如果您購買了某個產品,而我們還沒來得及更新,請聯繫站長或留言催更,謝謝理解 !
GG資源網 » WordPress建站教程:調整WordPress文章排序(WordPress分類與標籤等存檔頁實現置頂的方法)

發表回復

CAPTCHAis initialing...