要实现该功能,只需下载安装 Post Tags and Categories for Pages 插件。或者你将下面的代码(来自该插件)直接添加到当前主题的 functions.php 文件最后一个 ?> 的前面即可:
- //为 WordPress 页面添加标签和分类
- class PTCFP{
- function __construct(){
- add_action( 'init', array( $this, 'taxonomies_for_pages' ) );
- /**
- * 确保这些查询修改不会作用于管理后台,防止文章和页面混杂
- */
- if ( ! is_admin() ) {
- add_action( 'pre_get_posts', array( $this, 'category_archives' ) );
- add_action( 'pre_get_posts', array( $this, 'tags_archives' ) );
- } // ! is_admin
- } // __construct
- /**
- * 为“页面”添加“标签”和“分类”
- *
- * @uses register_taxonomy_for_object_type
- */
- function taxonomies_for_pages() {
- register_taxonomy_for_object_type( 'post_tag', 'page' );
- register_taxonomy_for_object_type( 'category', 'page' );
- } // taxonomies_for_pages
- /**
- * 在标签存档中包含“页面”
- */
- function tags_archives( $wp_query ) {
- if ( $wp_query->get( 'tag' ) )
- $wp_query->set( 'post_type', 'any' );
- } // tags_archives
- /**
- * 在分类存档中包含“页面”
- */
- function category_archives( $wp_query ) {
- if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
- $wp_query->set( 'post_type', 'any' );
- } // category_archives
- } // PTCFP
- $ptcfp = new PTCFP();
内容整理自:WordPress 大学 - https://www.wpdaxue.com/post-tags-and-categories-for-pages.html
经过懿古今测试,直接添加代码的方法比较简单有效,而且分类归档页和标签页确实包含了文章和页面。同理,我们可以拓展到为自定义类型文章添加分类和标签功能。
如若转载,请注明出处:https://www.ozabc.com/jianzhan/302167.html