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' ) );
溫馨提示:

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

文章連結:https://www.wuyanshuo.cn/1396.html

更新時間:2022年10月4日

1、本站所有資源均不添加推廣檔案或浮水印,壓縮包內若有廣告檔案和浮水印請勿輕易相信。

2、本站資源均為兩層壓縮,第一層7z(尾碼若為wys,請自行修改為7z)有解壓密碼; 第二層zip或cbz,無解壓密碼,可直接使用漫畫類軟件程式查看; 詳情可參攷解壓教程

3、本站大部分內容均收集於網絡! 若內容侵犯到您的權益,請發送郵件至:admin#wysacg.top我們將第一時間處理! 資源所需價格並非資源售賣價格,是收集、整理、編輯詳情以及本站運營的適當補貼,並且本站不提供任何免費技術支援。 所有資源僅限於參攷和學習,版權歸原作者所有!

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

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

2022-10-4 16:58:55

建站教程

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

2022-10-20 14:21:06

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索