在WordPress分类档案页面显示该分类置顶文章

知更鸟 WordPress评论127阅读模式

默认WordPress仅在首页显示置顶文章,那么要在分类档案页面显示该分类置顶文章,如何实现呢?可以尝试使用下面的代码。

文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

在WordPress分类档案页面显示该分类置顶文章

在WordPress分类档案页面显示该分类置顶文章

文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

将代码添加到当前主题函数模板 functions.php 中:文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

展开收缩
文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html
class StickyInCategory {
	/**
	 * A reference to an instance of this class.
	 */
	private static $instance;

	/**
	 * The array of categories that will have sticky posts moved to the top.
	 */
	protected $categories;
    
	/**
	 * Returns an instance of this class. 
	 */
	public static function get_instance() {

		if ( null == self::$instance ) {
			self::$instance = new StickyInCategory();
		}

		return self::$instance;

	}

	/**
	 * Initializes the plugin by setting filters and administration functions.
	 */
	private function __construct() {

		$this->categories = array();

		// Filter retrieved posts
		add_filter(	'the_posts', array( $this, 'prepend_sticky_posts' ), 10, 2 );
		
		// Set the 'sticky' class
		add_filter( 'post_class', array( $this, 'set_sticky_post_class' ), 10, 3 );
	}

	/**
	 * Move sticky posts to the top of the archive listing.
	 */
	public function prepend_sticky_posts( $posts, $query ) {

		if ( !is_admin() && is_main_query() ) {
			
			// Hook to initialise the categories if none specified.
			if ( empty( $this->categories ) ) {
				$this->categories = apply_filters( 'sia_sticky_categories', $this->categories );
			}

			// Only continue if we are viewing a category archive.
			if ( array_key_exists( 'category_name', $query->query_vars ) ) {
				$category_matched = false;
				if ( empty( $this->categories ) ) {
					// If no categories were supplied by the apply_filters() then operate on all categories.
					$category_matched = true;
				}
				else {
					// Check whether the current category is in the list.
					$category_matched = in_array( $query->query_vars['category_name'], $this->categories );
				}

				if ( $category_matched ) {
					// Copied from the bottom of WP_Query::get_posts() in wp-includes/class-wp-query.php
					$sticky_posts = get_option( 'sticky_posts' );
					$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 ] );
						}
					}
				}
			}
		}
		return $posts;
	}

	/**
	 * Set the 'sticky' post class. get_post_class() only does it on the home page.
	 */
	public function set_sticky_post_class( $classes, $class, $post_ID ) {
		
		// TODO: Consider whether to reference $this->categories.
		if ( is_archive() && is_sticky( $post_ID ) ) {
			$classes[] = 'sticky';
		}
		return $classes;
	}
}

add_action( 'after_setup_theme', array( 'StickyInCategory', 'get_instance' ) );

文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

相关插件

分类显示置顶文章插件:Category Sticky Post文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

在WordPress分类档案页面显示该分类置顶文章文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

插件虽然已多年未更新,但目前尚可使用。文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

唯一缺点,貌似不支持父子分类。文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

 文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

 文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html 文章源自知更鸟-https://zmingcx.com/sticky-posts-in-category-archives.html

weinxin
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定