2024 年 4 月 28 日 我的频道
Wordpress 分类支持置顶文章
  • 2023
  • Sunny

通常的文章置顶功能只在主页显示,而 wordpress 默认情况下,分类是不支持置顶功能的。针对这一点,我们利用纯代码实现这个功能。针对这个功能,在分类页面用上 $wp_Query 循环即可,方便实现,。把下面的代码添加到主题的 Functions.php

//支持分类置顶文章
add_filter( 'the_posts', 'putStickyOnTop' );

function putStickyOnTop( $posts ) {
  if ( is_category() && is_main_query() ) {
    global $wp_query;

    // 获取所有置顶文章
    $sticky_posts = get_option( 'sticky_posts' );

    if ( $wp_query->query_vars[ 'paged' ] <= 1 && !empty( $sticky_posts ) && is_array( $sticky_posts ) && !get_query_var( 'ignore_sticky_posts' ) ) {
      $stickies = get_posts( array( 'post__in' => $sticky_posts ) );

      $allowed_categories = array();

      if ( $wp_query->is_category == 1 ) {
        $category_id = $wp_query->query_vars[ 'cat' ];
        $category = get_category( $category_id );
        $allowed_categories = get_term_children( $category->term_id, $category->taxonomy );
        $allowed_categories[] = $category->term_id;
      }

      foreach ( $stickies as $sticky_post ) {
        // 判断当前是否分类页
        if ( $wp_query->is_category == 1 && !has_category( $allowed_categories, $sticky_post->ID ) ) {
          // 去除不属于本分类的置顶文章
          $offset = array_search( $sticky_post->ID, $sticky_posts );
          unset( $sticky_posts[ $offset ] );
        }
      }

      $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 ( !empty( $sticky_posts ) && !empty( $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 ( !empty( $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;
}

add_filter( 'the_posts', 'putStickyOnTop' );

分类页面循环代码:

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$sticky_posts = get_option('sticky_posts');
    global $wp_query;

    if ($wp_query->have_posts()) :
      while ($wp_query->have_posts()) : $wp_query->the_post();
      ?>
开始您的循环代码
<?php
endwhile; 
endif;
wp_reset_postdata();
?>

大家可根据自身要求对代码进行二次修改开发,本代码源自互联网《露兜博客》,本站加以完善代码并且利用!

STUDY
917
0

Author Box

名字:Sunny
注册:Jan 30, 2021
简介:技术菜鸟,拷贝、黏贴代码中 ……

Comment Box

0 0 投票数
Article Rating
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论