开发者

WordPress: add a customized Category Manager to the *PAGE* editor

In WordPress, the post editor contains a Categories panel that allows you to assign categories to posts. However, the page editor does not have a Categories panel. I'm sure this is by design, but I need a modified set of category items to be available to pages.

I've found that I can add the line of code below to my theme's functions.php file in order to add the missing categories selector to the page editor...

add_action('admin_menu', 'my_post_categories_meta_box');
function my_post_categories_meta_box() {
    add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'core');
}

However, I'd like to specify开发者_运维知识库 the categories that should appear in this menu. I don't want it to list all of the available categories, only those that I choose to include. Is this possible?

If not, how would I add a widget which essentially does the same thing (provides a list of items with checkboxes next to them)?


The best way is to make your own meta box in the editor window, then filter out the categories or define which ones you want displayed manually.

To get an array of categories is simple use wordpress' get_categories function to obtain an array of categories, then if you want to remove some from the options then just unset them from that array.

This is a short extract from what i placed in my functions.php, essentially this linked to my own php file which contained code for selecting a category, and then saving it.

This first shows how to make your custom editing section.

add_action('admin_menu', 'custom_admin');
/* Adds a custom section to the "side" of the post edit screen */
function custom_admin() {
     add_meta_box('category_selector', 'Settings', 'category_custom_box', 'post', 'side', 'low');

/* prints the custom field in the new custom post section */
function category_custom_box() {
    //get post meta values

    global $post;
    //$currentCat gets the pages current category id
    $currentCat = wp_get_post_categories($post->ID);

    //Do your printing of the form here.
}

And then to save the category make a new function and add it to the 'save_post' hook.

/* when the post is saved, save the custom data */
function save_postdata($post_id) {
        // verify this with nonce because save_post can be triggered at other times
        if (!wp_verify_nonce($_POST['customCategory_noncename'], 'customCategory')) return $post_id;

        // do not save if this is an auto save routine
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

        //get the category and set it
        $custom_category = $_POST['custom_category'];
        wp_set_post_categories($post_id, array($custom_category));

 ...
 }

The nonce value is just a randomly generated string to check that the session is valid, avoiding concurrency, to make one just add this to your form,

<input type="hidden" name="customCategory_noncename" id="customCategory_noncename" value="<?= wp_create_nonce('customCategory'); ?>" />

Sorry about the amount of code, i tried to slim it down as much as poss.

Hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜