!
也想出现在这里? 联系我们
广告位

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

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

  1. class StickyInCategory {
  2. /**
  3. * A reference to an instance of this class.
  4. */
  5. private static $instance;
  6. /**
  7. * The array of categories that will have sticky posts moved to the top.
  8. */
  9. protected $categories;
  10. /**
  11. * Returns an instance of this class.
  12. */
  13. public static function get_instance() {
  14. if ( null == self::$instance ) {
  15. self::$instance = new StickyInCategory();
  16. }
  17. return self::$instance;
  18. }
  19. /**
  20. * Initializes the plugin by setting filters and administration functions.
  21. */
  22. private function __construct() {
  23. $this->categories = array();
  24. // Filter retrieved posts
  25. add_filter( 'the_posts', array( $this, 'prepend_sticky_posts' ), 10, 2 );
  26. // Set the 'sticky' class
  27. add_filter( 'post_class', array( $this, 'set_sticky_post_class' ), 10, 3 );
  28. }
  29. /**
  30. * Move sticky posts to the top of the archive listing.
  31. */
  32. public function prepend_sticky_posts( $posts, $query ) {
  33. if ( !is_admin() && is_main_query() ) {
  34. // Hook to initialise the categories if none specified.
  35. if ( empty( $this->categories ) ) {
  36. $this->categories = apply_filters( 'sia_sticky_categories', $this->categories );
  37. }
  38. // Only continue if we are viewing a category archive.
  39. if ( array_key_exists( 'category_name', $query->query_vars ) ) {
  40. $category_matched = false;
  41. if ( empty( $this->categories ) ) {
  42. // If no categories were supplied by the apply_filters() then operate on all categories.
  43. $category_matched = true;
  44. }
  45. else {
  46. // Check whether the current category is in the list.
  47. $category_matched = in_array( $query->query_vars['category_name'], $this->categories );
  48. }
  49. if ( $category_matched ) {
  50. // Copied from the bottom of WP_Query::get_posts() in wp-includes/class-wp-query.php
  51. $sticky_posts = get_option( 'sticky_posts' );
  52. $num_posts = count( $posts );
  53. $sticky_offset = 0;
  54. // Loop over posts and relocate stickies to the front.
  55. for ( $i = 0; $i < $num_posts; $i++ ) {
  56. if ( in_array( $posts[ $i ]->ID, $sticky_posts ) ) {
  57. $sticky_post = $posts[ $i ];
  58. // Remove sticky from current position
  59. array_splice( $posts, $i, 1 );
  60. // Move to front, after other stickies
  61. array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
  62. // Increment the sticky offset. The next sticky will be placed at this offset.
  63. $sticky_offset++;
  64. // Remove post from sticky posts array
  65. $offset = array_search( $sticky_post->ID, $sticky_posts );
  66. unset( $sticky_posts[ $offset ] );
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return $posts;
  73. }
  74. /**
  75. * Set the 'sticky' post class. get_post_class() only does it on the home page.
  76. */
  77. public function set_sticky_post_class( $classes, $class, $post_ID ) {
  78. // TODO: Consider whether to reference $this->categories.
  79. if ( is_archive() && is_sticky( $post_ID ) ) {
  80. $classes[] = 'sticky';
  81. }
  82. return $classes;
  83. }
  84. }
  85. add_action( 'after_setup_theme', array( 'StickyInCategory', 'get_instance' ) );
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

SuperPWA – 让你的WordPress网站瞬间变成APP

2022-10-4 16:58:55

WordPress教程

Gutenberg 古登堡编辑器开启自定义字段模块方法

2022-10-20 14:21:06

!
也想出现在这里? 联系我们
广告位
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索