如何将页面“分组”(分类)在一起?

时间:2015-01-14 作者:夏期劇場

我有很多Pages 可以(需要)分为许多不同的类别。**更重要的是,我需要通过functions.php.

比如说,我会:

第A页(分类为:Fruits)Vehicles)Vehicles)Fruits)Technology)然后从functions.php, 会有一些逻辑,比如:

如果页面位于Fruits 类别,然后echo "This is related to Fruits.";.Vehicles 类别,然后echo "This is related to Vehicles.";.Technology 类别,然后echo "This is related to Technologies.";.我不知道这是关于分类法、标签或自定义字段之类的。但是:

  • What is the ideal way to do it please? (Especially to be able to control from backend coding.)let me repeat, "Pages". (不涉及帖子或任何其他内容)

2 个回复
SO网友:Milo

通过注册页面对象类型的类别分类法,可以将类别用于页面:

function categories_for_pages(){
    register_taxonomy_for_object_type( \'category\', \'page\' );
}
add_action( \'init\', \'categories_for_pages\' );
如果要对此使用单独的分类法,可以register your own taxonomy 对于页。

SO网友:JesusIniesta

  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 (以Wordpress$post命名):
    if ($post->post_type == \'page\'){
        if (has_category( \'Fruits\', $post)){
            echo "This is related to Fruits.";
        }
    }
    

结束