腾讯云服务器优惠活动华纳云香港服务器

如何为WordPress页面(page)添加标签和分类功能?

WordPress主题推荐

要实现该功能,只需下载安装 Post Tags and Categories for Pages 插件。或者你将下面的代码(来自该插件)直接添加到当前主题的 functions.php 文件最后一个 ?> 的前面即可:

  1. //为 WordPress 页面添加标签和分类
  2. class PTCFP{
  3.   function __construct(){
  4.     add_action( ‘init’, array$this, ‘taxonomies_for_pages’ ) );
  5.     /**
  6.      * 确保这些查询修改不会作用于管理后台,防止文章和页面混杂 
  7.      */
  8.     if ( ! is_admin() ) {
  9.       add_action( ‘pre_get_posts’, array$this, ‘category_archives’ ) );
  10.       add_action( ‘pre_get_posts’, array$this, ‘tags_archives’ ) );
  11.     } // ! is_admin
  12.   } // __construct
  13.   /**
  14.    * 为“页面”添加“标签”和“分类”
  15.    *
  16.    * @uses register_taxonomy_for_object_type
  17.    */
  18.   function taxonomies_for_pages() {
  19.       register_taxonomy_for_object_type( ‘post_tag’, ‘page’ );
  20.       register_taxonomy_for_object_type( ‘category’, ‘page’ );
  21.   } // taxonomies_for_pages
  22.   /**
  23.    * 在标签存档中包含“页面”
  24.    */
  25.   function tags_archives( $wp_query ) {
  26.     if ( $wp_query->get( ‘tag’ ) )
  27.       $wp_query->set( ‘post_type’, ‘any’ );
  28.   } // tags_archives
  29.   /**
  30.    * 在分类存档中包含“页面”
  31.    */
  32.   function category_archives( $wp_query ) {
  33.     if ( $wp_query->get( ‘category_name’ ) || $wp_query->get( ‘cat’ ) )
  34.       $wp_query->set( ‘post_type’, ‘any’ );
  35.   } // category_archives
  36. // PTCFP
  37. $ptcfp = new PTCFP();

以上内容整理自@WordPress 大学

经过懿古今测试,直接添加代码的方法比较简单有效,而且分类归档页和标签页确实包含了文章和页面。同理,我们可以拓展到为自定义类型文章添加分类和标签功能。

本文地址:https://boke112.com/post/4767.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
香港云主机55元/年
wu