建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

让 WordPress 页面支持添加分类及标签

GG网络技术分享 2025-03-18 16:05 0


默认 页面不支持选择分类和添加标签,可以通过下面的方法为页面添加分类及标签。将下面代码添加到当前函数模板 functions.php 中:

  1.     function san_add_taxonomies_to_pages() {
  2.     register_taxonomy_for_object_type( 'post_tag', 'page' );
  3.     register_taxonomy_for_object_type( 'category', 'page' );
  4.     }
  5.  
  6.     add_action( 'init', 'san_add_taxonomies_to_pages' );
  7.     if ( ! is_admin() ) {
  8.     add_action( 'pre_get_posts', 'san_category_and_tag_archives' );
  9.     }
  10.  
  11.     function san_category_and_tag_archives( $wp_query ) {
  12.     $my_san_post_array = array('post','page');
  13.     if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
  14.     $wp_query->set( 'post_type', $my_san_post_array );
  15.  
  16.     if ( $wp_query->get( 'tag' ) )
  17.     $wp_query->set( 'post_type', $my_san_post_array );
  18.     }

之后,页面与正常文章一样可以选择添加分类和标签了。

代码取自

也可以直接安装上述插件。

标签: WordPress 教程

提交需求或反馈

Demand feedback