国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress分類/標(biāo)簽等存檔頁也能置頂文章方法

2024-09-07 00:50:02
字體:
供稿:網(wǎng)友

在wordpress中默認(rèn)能置頂文章就是只有首頁了,如果我們希望分類/標(biāo)簽等存檔頁也能置頂文章我們需要二次開發(fā).

現(xiàn)在參考wp-includes/query.php中首頁置頂?shù)拇a,稍微修改一下,可以讓分類頁、標(biāo)簽頁、作者頁和日期頁等存檔頁面也能像首頁一樣在頂部顯示其范圍內(nèi)的置頂文章,把下面的代碼放到當(dāng)前主題下的functions.php中就可以了.

  1. add_filter('the_posts',  'putStickyOnTop' ); 
  2. function putStickyOnTop( $posts ) { 
  3.   if(is_home() || !is_main_query() || !is_archive()) 
  4.     return $posts
  5.      
  6.   global $wp_query
  7.   $sticky_posts = get_option('sticky_posts'); 
  8.    
  9.   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 ) ); 
  10.     foreach ( $stickies1 as $sticky_post1 ) { 
  11.       // 判斷當(dāng)前是否分類頁  
  12.       if($wp_query->is_category == 1 && !has_category($wp_query->query_vars['cat'], $sticky_post1->ID)) { 
  13.         // 去除不屬于本分類的文章 
  14.         $offset1 = array_search($sticky_post1->ID, $sticky_posts); 
  15.         unset( $sticky_posts[$offset1] ); 
  16.       } 
  17.       if($wp_query->is_tag == 1 && has_tag($wp_query->query_vars['tag'], $sticky_post1->ID)) { 
  18.         // 去除不屬于本標(biāo)簽的文章 
  19.         $offset1 = array_search($sticky_post1->ID, $sticky_posts); 
  20.         unset( $sticky_posts[$offset1] ); 
  21.       } 
  22.       if($wp_query->is_year == 1 && date_i18n('Y'strtotime($sticky_post1->post_date))!=$wp_query->query['m']) { 
  23.         // 去除不屬于本年份的文章 
  24.         $offset1 = array_search($sticky_post1->ID, $sticky_posts); 
  25.         unset( $sticky_posts[$offset1] ); 
  26.       } 
  27.       if($wp_query->is_month == 1 && date_i18n('Ym'strtotime($sticky_post1->post_date))!=$wp_query->query['m']) { 
  28.         // 去除不屬于本月份的文章 
  29.         $offset1 = array_search($sticky_post1->ID, $sticky_posts); 
  30.         unset( $sticky_posts[$offset1] ); 
  31.       } 
  32.       if($wp_query->is_day == 1 && date_i18n('Ymd'strtotime($sticky_post1->post_date))!=$wp_query->query['m']) { 
  33.         // 去除不屬于本日期的文章 
  34.         $offset1 = array_search($sticky_post1->ID, $sticky_posts); 
  35.         unset( $sticky_posts[$offset1] ); 
  36.       } 
  37.       if($wp_query->is_author == 1 && $sticky_post1->post_author != $wp_query->query_vars['author']) { 
  38.         // 去除不屬于本作者的文章 
  39.         $offset1 = array_search($sticky_post1->ID, $sticky_posts); 
  40.         unset( $sticky_posts[$offset1] ); 
  41.       } 
  42.     } 
  43.    
  44.     $num_posts = count($posts); 
  45.     $sticky_offset = 0; 
  46.     // Loop over posts and relocate stickies to the front. 
  47.     for ( $i = 0; $i < $num_posts$i++ ) { 
  48.       if ( in_array($posts[$i]->ID, $sticky_posts) ) { 
  49.         $sticky_post = $posts[$i]; 
  50.         // Remove sticky from current position 
  51.         array_splice($posts$i, 1); 
  52.         // Move to front, after other stickies 
  53.         array_splice($posts$sticky_offset, 0, array($sticky_post)); 
  54.         // Increment the sticky offset. The next sticky will be placed at this offset. 
  55.         $sticky_offset++; 
  56.         // Remove post from sticky posts array 
  57.         $offset = array_search($sticky_post->ID, $sticky_posts); 
  58.         unset( $sticky_posts[$offset] ); 
  59.       } 
  60.     } 
  61.     // If any posts have been excluded specifically, Ignore those that are sticky. 
  62.     if ( !emptyempty($sticky_posts) && !emptyempty($wp_query->query_vars['post__not_in'] ) ) 
  63.       $sticky_posts = array_diff($sticky_posts$wp_query->query_vars['post__not_in']); 
  64.     // Fetch sticky posts that weren't in the query results 
  65.     if ( !emptyempty($sticky_posts) ) { 
  66.       $stickies = get_posts( array
  67.         'post__in' => $sticky_posts
  68.         'post_type' => $wp_query->query_vars['post_type'], 
  69.         'post_status' => 'publish'
  70.         'nopaging' => true 
  71.       ) ); 
  72.       foreach ( $stickies as $sticky_post ) { 
  73.         array_splice$posts$sticky_offset, 0, array$sticky_post ) ); 
  74.         $sticky_offset++; 
  75.       } 
  76.     } 
  77.   } 
  78.    
  79.   return $posts

代碼說明:

1、如果你想讓存檔頁也都顯示全部置頂文章,那么就刪掉11-43行的代碼;

2、如果不想在某分類頁顯示置頂文章,將第 3 行的

  1. if(  
  2. //改成: 
  3. // abc是分類名稱 
  4. if ( is_category( 'abc' ) || 

3、如果不想某標(biāo)簽頁顯示置頂文章,將第 3 行的代碼

  1. if(  
  2. //改成: 
  3. // abc是標(biāo)簽名稱 
  4. if ( is_tag( 'abc' ) || 

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

  1. if
  2. //改成: 
  3.  // abc是作者昵稱 
  4. if ( is_author( 'abc' ) || 

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

代碼如下:|| !is_main_query() 

置頂樣式:如果你想給置頂文章添加樣式,將以下代碼添加到functions.php中,會給置頂文章添加一個名為 sticky 的class,具體的css代碼,再自行自定義:

  1. add_filter('post_class',  'addStickyClass' ,10,3 ); 
  2. function addStickyClass( $classes$class$post_id ){ 
  3.   if( is_sticky() && is_category() && !isset( $classes['sticky'] ) ){ 
  4.     $classes[] = 'sticky'
  5.   } 
  6.    
  7.   return $classes

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 白河县| 溧水县| 泾源县| 繁峙县| 青河县| 湟源县| 建德市| 东港市| 商水县| 崇文区| 沛县| 从江县| 邓州市| 丹凤县| 吉木乃县| 张北县| 新和县| 柳州市| 渝中区| 师宗县| 铜梁县| 福安市| 临沧市| 同江市| 六枝特区| 苍南县| 翼城县| 临洮县| 突泉县| 辽源市| 颍上县| 西青区| 定南县| 绥宁县| 宁河县| 尉犁县| 句容市| 临清市| 天气| 桓台县| 焉耆|