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

WordPress的页面(page)默认是不支持添加标签和分类的,下面将分享下,让页面支持添加标签和分类,以及在标签存档和分类存档中包含页面的方法。

post-tags-and-categories-for-pages-wpdaxue_com

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//为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页面添加标签和分类 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();

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号