admin管理员组

文章数量:1122846

I have a lot of Pages which can be (need to be) grouped under a number of different Categories. ** Then the more important thing is, i need to control those pages (of different groups) programatically via functions.php.

Lets say, i would have:

  • Page A (Categorized as: Fruits)
  • Page B (Categorized as: Vehicles)
  • Page C (Categorized as: Vehicles)
  • Page D (Categorized as: Fruits)
  • Page E (Categorized as: Technology)

Then from functions.php, there would be some logics, like:

  • If Page is under Fruits Category, then echo "This is related to Fruits.";.
  • If Page is under Vehicles Category, then echo "This is related to Vehicles.";.
  • If Page is under Technology Category, then echo "This is related to Technologies.";.

I dont know this is about Taxonomy or Tagging or Custom Field or something. But:

  • What is the ideal way to do it please? (Especially to be able to control from backend coding.)

And again please let me repeat, "Pages". (Not about Posts or any other)

I have a lot of Pages which can be (need to be) grouped under a number of different Categories. ** Then the more important thing is, i need to control those pages (of different groups) programatically via functions.php.

Lets say, i would have:

  • Page A (Categorized as: Fruits)
  • Page B (Categorized as: Vehicles)
  • Page C (Categorized as: Vehicles)
  • Page D (Categorized as: Fruits)
  • Page E (Categorized as: Technology)

Then from functions.php, there would be some logics, like:

  • If Page is under Fruits Category, then echo "This is related to Fruits.";.
  • If Page is under Vehicles Category, then echo "This is related to Vehicles.";.
  • If Page is under Technology Category, then echo "This is related to Technologies.";.

I dont know this is about Taxonomy or Tagging or Custom Field or something. But:

  • What is the ideal way to do it please? (Especially to be able to control from backend coding.)

And again please let me repeat, "Pages". (Not about Posts or any other)

Share Improve this question edited Jan 14, 2015 at 15:01 夏期劇場 asked Jan 14, 2015 at 10:03 夏期劇場夏期劇場 5063 gold badges11 silver badges24 bronze badges 4
  • 1 Use posts with categories and tags or custom post types with custom taxonomies, it is the best way of doing what you are describing. – cybmeta Commented Jan 14, 2015 at 10:55
  • Thank you but I think my question is about Pages. – 夏期劇場 Commented Jan 14, 2015 at 15:02
  • 1 I know that your question is about pages, I just made a suggestion of data structure that can fit better the description of your issue. – cybmeta Commented Jan 14, 2015 at 15:46
  • 1 @cybmeta I agree, if you need to categorize "pages" then you should create a Custom Post Type and Custom Taxonomy. The built-in Pages were not designed to have categories or be categorized. A Custom Post Type can be a page or a post or anything you want. Read More about Post Types. Another option is create a Parent Page called "Fruits" then make subpages, the parent page would act as a Category, though it truely isn't. – Howdy_McGee Commented Jan 14, 2015 at 18:02
Add a comment  | 

2 Answers 2

Reset to default 0

You can use categories with pages by registering the category taxonomy for the page object type:

function categories_for_pages(){
    register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'categories_for_pages' );

If you want to use a separate taxonomy for this, you can register your own taxonomy for pages.

  1. First thing you need to do is to enable categories for pages, quite simple:

    function enable_categories_for_pages() {
        register_taxonomy_for_object_type('category', 'page');  
    }
    add_action( 'init', 'enable_categories_for_pages' );
    

Note that in WordPress, categories are shared between different post types

  1. To register the categories in advance:
    $wp_insert_category(array(
        'cat_name' => 'Fruit',
        'category_description' => 'My Fruit posts',
        'category_nicename' => 'Fruit'
    ));
    
  2. You can add categories to posts programmatically:
    wp_set_post_categories($post->ID, get_cat_ID( 'Fruit' ));
    
  3. When displaying the page (named in Wordpress $post):
    if ($post->post_type == 'page'){
        if (has_category( 'Fruits', $post)){
            echo "This is related to Fruits.";
        }
    }
    

本文标签: categoriesHow to quotgroupquot (categorize) the Pages together