loop - How to insert category list into post creation page, and retrieve chosen categories?

admin2025-06-04  10

I would like to display the full list of categories on the Page creation page, so that the user can pick using checkboxes which category posts should appear on that page.

I then need to retrieve the chosen categories for that specific page in my custom page templates, so that I can modify my loop to filter for only those chosen categories.

I would like to display the full list of categories on the Page creation page, so that the user can pick using checkboxes which category posts should appear on that page.

I then need to retrieve the chosen categories for that specific page in my custom page templates, so that I can modify my loop to filter for only those chosen categories.

Share Improve this question edited Jan 10, 2019 at 8:52 Glorfindel 6113 gold badges10 silver badges18 bronze badges asked Feb 7, 2013 at 9:25 KayoKayo 1831 silver badge9 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

By default page post type doesn't support category taxonomy. But you can easily fix it by register category taxonomy for page post type:

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

Now you will see categories meta box in the same place as you see it at post creation page.

To get page's categories just call wp_get_post_categories function like this (thanks to @Subharanjan):

$page_categories = wp_get_post_categories( get_the_ID() ); 
$page_categories = wp_get_post_categories( get_the_ID() ); 

Pass the current page id/ any pageid as the param and get the asociated categories.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749026427a315734.html

最新回复(0)