最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Is it possible to arrange Custom Post Types from CPTUI into a Folder?

programmeradmin3浏览0评论

I'm using CPTUI to create taxonomies and categories for Facet-WP, which i'm using to sort and filter products on the pages i'm creating. This is the problem i'm facing :-

As you can see, the Custom post types on the left have completely overwhelmed the section on the left (Inductive_proximities). Is there a way to put these into some custom page or dropdown, so as to avoid cluttering this page? I have to create about a 100 more of these and i'm dreading the mess this will create.

Thanks

I'm using CPTUI to create taxonomies and categories for Facet-WP, which i'm using to sort and filter products on the pages i'm creating. This is the problem i'm facing :-

As you can see, the Custom post types on the left have completely overwhelmed the section on the left (Inductive_proximities). Is there a way to put these into some custom page or dropdown, so as to avoid cluttering this page? I have to create about a 100 more of these and i'm dreading the mess this will create.

Thanks

Share Improve this question asked Mar 29, 2019 at 5:12 Prateek PandeyPrateek Pandey 53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There are multiple ways to remove or re-order these. I've created couple of post types (Test1, Test2, Test3) to test following ways.

CPT UI configuration way

You can add new post types with Settings > Show in menu set to False and the post type will not appear in menu at all.

Plugin way

You might want to try plugin such as Admin Menu Editor (https://wordpress/plugins/admin-menu-editor/) in combination with previous setting, that will let drag and drop edit your menu and add the links to the post types that don't show in menu.

Manual code way

You can achieve same results as above by hardcoding this into custom plugin or your theme's functions.php for example. The manual way would filter register_post_type_args hook.

function wp332896_custom_post_type_args( $args, $post_type ) {
    $post_types_to_not_show_in_menu = ['test1', 'test2', 'test3'];
    if ( in_array($post_type, $post_types_to_not_show_in_menu) ) {
        $args['show_in_menu'] = false;
    }

    return $args;
}
add_filter( 'register_post_type_args', 'wp332896_custom_post_type_args', 20, 2 );

Note that this is basically the same as using CPT UI configuration to change Show in menu value to false, just wrote to code. Variable $post_types_to_not_show_in_menu holds array of post types that are not to be shown in menu.

Dynamic code way

If you wanted to do this for all the post types registered with CPT UI, we could use CPT UI's function cptui_get_post_type_data().

function wp332896_custom_post_type_args( $args, $post_type ) {

    $post_types_to_not_show_in_menu = [];

    if (function_exists('cptui_get_post_type_data')) {
        $post_types_to_not_show_in_menu = array_column(cptui_get_post_type_data(), 'name');
    }

    if ( in_array($post_type, $post_types_to_not_show_in_menu) ) {
        $args['show_in_menu'] = false;
    }

    return $args;
}
add_filter( 'register_post_type_args', 'wp332896_custom_post_type_args', 20, 2 );

Note that $post_types_to_not_show_in_menu now uses that function to get all names of post types registered with CPT UI.

To finish it, you can add "folder" with these post types as submenu. I'll connect the folder and the submenu pages by using string "cptfolder", but you can have just about any.

function wp332896_folder_menu() {

    $menu_slug = 'cptfolder';

    add_menu_page('Folder', 'Folder', 'manage_options', $menu_slug);

    $cpt_post_types = [];

    if (function_exists('cptui_get_post_type_data')) {
        $cpt_post_types = cptui_get_post_type_data();
    }

    foreach($cpt_post_types as $post_type) {
        add_submenu_page($menu_slug, $post_type['label'], $post_type['label'], 'manage_options', 'edit.php?post_type='.$post_type['name']);
    }
 }

add_action('admin_menu', 'wp332896_folder_menu');

发布评论

评论列表(0)

  1. 暂无评论