当前位置:首页文章笔记建站教程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. * 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月04日

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

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

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

2022-10-4 16:58:55

建站教程

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

2022-10-20 14:21:06

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