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, thenecho "This is related to Fruits.";
. - If Page is under
Vehicles
Category, thenecho "This is related to Vehicles.";
. - If Page is under
Technology
Category, thenecho "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, thenecho "This is related to Fruits.";
. - If Page is under
Vehicles
Category, thenecho "This is related to Vehicles.";
. - If Page is under
Technology
Category, thenecho "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 |2 Answers
Reset to default 0You 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.
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
- To register the categories in advance:
$wp_insert_category(array( 'cat_name' => 'Fruit', 'category_description' => 'My Fruit posts', 'category_nicename' => 'Fruit' ));
- You can add categories to posts programmatically:
wp_set_post_categories($post->ID, get_cat_ID( 'Fruit' ));
- 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
版权声明:本文标题:categories - How to "group" (categorize) the Pages together? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284431a1927248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Pages
. – 夏期劇場 Commented Jan 14, 2015 at 15:02