$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - How to insert category list into post creation page, and retrieve chosen categories?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far