WordPress教程

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

也想出现在这里?联系我们
创客云

默认 WordPress 仅在首页显示置顶文章,如想在分类档案页面显示该分类置顶文章,可以尝试使用下面的代码。将代码添加到当前主题函数模板 functions.php 中:

  1.     class StickyInCategory {
  2.     	/**
  3.     	 * A reference to an instance of this class.
  4.     	 */
  5.     	private static $instance;
  6.  
  7.     	/**
  8.     	 * The array of categories that will have sticky posts moved to the top.
  9.     	 */
  10.     	protected $categories;
  11.  
  12.     	/**
  13.     	 * Returns an instance of this class. 
  14.     	 */
  15.     	public static function get_instance() {
  16.  
  17.     		if ( null == self::$instance ) {
  18.     			self::$instance = new StickyInCategory();
  19.     		}
  20.  
  21.     		return self::$instance;
  22.  
  23.     	}
  24.  
  25.     	/**
  26.     	 * Initializes the plugin by setting filters and administration functions.
  27.     	 */
  28.     	private function __construct() {
  29.  
  30.     		$this->categories = array();
  31.  
  32.     		// Filter retrieved posts
  33.     		add_filter(	'the_posts', array( $this, 'prepend_sticky_posts' ), 10, 2 );
  34.  
  35.     		// Set the 'sticky' class
  36.     		add_filter( 'post_class', array( $this, 'set_sticky_post_class' ), 10, 3 );
  37.     	}
  38.  
  39.     	/**
  40.     	 * Move sticky posts to the top of the archive listing.
  41.     	 */
  42.     	public function prepend_sticky_posts( $posts, $query ) {
  43.  
  44.     		if ( !is_admin() && is_main_query() ) {
  45.  
  46.     			// Hook to initialise the categories if none specified.
  47.     			if ( empty( $this->categories ) ) {
  48.     				$this->categories = apply_filters( 'sia_sticky_categories', $this->categories );
  49.     			}
  50.  
  51.     			// Only continue if we are viewing a category archive.
  52.     			if ( array_key_exists( 'category_name', $query->query_vars ) ) {
  53.     				$category_matched = false;
  54.     				if ( empty( $this->categories ) ) {
  55.     					// If no categories were supplied by the apply_filters() then operate on all categories.
  56.     					$category_matched = true;
  57.     				}
  58.     				else {
  59.     					// Check whether the current category is in the list.
  60.     					$category_matched = in_array( $query->query_vars['category_name'], $this->categories );
  61.     				}
  62.  
  63.     				if ( $category_matched ) {
  64.     					// Copied from the bottom of WP_Query::get_posts() in wp-includes/class-wp-query.php
  65.     					$sticky_posts = get_option( 'sticky_posts' );
  66.     					$num_posts = count( $posts );
  67.     					$sticky_offset = 0;
  68.     					// Loop over posts and relocate stickies to the front.
  69.     					for ( $i = 0; $i < $num_posts; $i++ ) {
  70.     						if ( in_array( $posts[ $i ]->ID, $sticky_posts ) ) {
  71.     							$sticky_post = $posts[ $i ];
  72.     							// Remove sticky from current position
  73.     							array_splice( $posts, $i, 1 );
  74.     							// Move to front, after other stickies
  75.     							array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
  76.     							// Increment the sticky offset. The next sticky will be placed at this offset.
  77.     							$sticky_offset++;
  78.     							// Remove post from sticky posts array
  79.     							$offset = array_search( $sticky_post->ID, $sticky_posts );
  80.     							unset( $sticky_posts[ $offset ] );
  81.     						}
  82.     					}
  83.     				}
  84.     			}
  85.     		}
  86.     		return $posts;
  87.     	}
  88.  
  89.     	/**
  90.     	 * Set the 'sticky' post class. get_post_class() only does it on the home page.
  91.     	 */
  92.     	public function set_sticky_post_class( $classes, $class, $post_ID ) {
  93.  
  94.     		// TODO: Consider whether to reference $this->categories.
  95.     		if ( is_archive() && is_sticky( $post_ID ) ) {
  96.     			$classes[] = 'sticky';
  97.     		}
  98.     		return $classes;
  99.     	}
  100.     }
  101.  
  102.     add_action( 'after_setup_theme', array( 'StickyInCategory', 'get_instance' ) );
也想出现在这里?联系我们
创客主机
资源下载此资源下载价格为5立即购买,VIP 免费

收藏
(0)

本文由 云模板 作者:PetitQ 发表,转载请注明来源!

也想出现在这里?联系我们
创客主机

发表回复

热销模板

Hub - 响应式可视化编辑企业官网WordPress汉化模板

Envato

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!

请扫码添加QQ好友后咨询!