Display list of Custom Post
-
I created Custom Post Type
News
. I would like to show the list of Custom PostNews
at a sub menu ofNews Info
. How can I do that ?- This topic was modified 2 years, 5 months ago by Jan Dembowski.
-
For a no-code solution, use a dynamic block plugin like PostX: https://www.ads-software.com/plugins/ultimate-post/
In your screenshot you can already see this list. If you add entries, they will be in this list. This list is accessible in the backend by clicking on “News Info”.
Or do you want to make this list available as a submenu item of “News Info”? Then the question would be how you set up your custom post type: via PHP (with which code?) or with the help of a plugin (which one)?
Thanks @gappiah . I am learning plugin development. That’s why I need coding solution. Thanks.
Thanks @threadi . Yes, I want to make this list available as a submenu item of “News Info”. Sorry for my poor English.
I am learning plugin development. That’s why I need coding solution. Thanks.
You are searching for: https://developer.www.ads-software.com/reference/functions/add_submenu_page/
There are also example for the usage in this page.
Right, add_submenu_page() adds sub menus. But doesn’t your CPT already have a submenu item to list news posts? It should. It’ll have the same label as the main menu item though. Adding another submenu will appear below the default News Info and Add New submenu items unless you use the right $position arg.
If you want to change the label of the default item, you’d need to remove the default item and replace with your own. To get a submenu item go to an URL instead of executing a callback, omit the callback arg and pass the URL relative to /wp-admin/ as the $menu_slug arg.
Or a little hacky but more direct, alter the appropriate label in the global $submenu array.
No, with a custom posttype a submenu is only created when you add submenus yourself or assign taxonomies that are enabled for the menu. As long as neither of these is the case, no submenu will appear.
If you absolutely want to have such an expanding menu without one of the above being true, then this can only be done by manual addition via add_submenu_page().
Source: own experience, only recently ??
Thanks @bcworkz . Yes, My CPT already have a submenu item to list news posts but I would like to attach it with another menu.
My custom post type registration code is like below
public function register_custom_post_type () { $supports = array('title'); $labels = array( 'name' => _x('News', 'plural'), 'singular_name' => _x('News', 'singular'), 'name_admin_bar' => _x('News', 'admin bar'), 'add_new' => _x('Add New', 'add new'), 'add_new_item' => __('Add New News'), 'new_item' => __('New News'), 'edit_item' => __('Edit News'), 'view_item' => __('View News'), 'all_items' => __('All Newss'), 'search_items' => __('Search News'), 'not_found' => __('No News Found.'), ); $args = array( 'supports' => $supports, 'labels' => $labels, 'public' => true, 'description' => 'News Information', 'has_archive' => true, 'exclude_from_search' => false, 'show_in_menu' => 'edit.php?post_type=news' ); register_post_type('news', $args); }
My Menu code is like below
public function admin_menu() { add_menu_page(‘News Information’, ‘News Info’, ‘manage_options’, ‘news_info’, [$this, ‘api_key_page’], ‘dashicons-info-outline’, 30); add_submenu_page(‘news_info’, ‘News Information’, ‘API Key’, ‘manage_options’, ‘news_info_page’, [$this, ‘api_key_page’]); add_submenu_page(‘news_info’, ‘News Information’, ‘News’, ‘manage_options’, ‘edit.php?post_type=news’); remove_submenu_page(‘news_info’, ‘news_info’); }
Thanks @threadi . Yes, I want to have such an expanding menu.
My custom post type registration code is like below
public function register_custom_post_type () { $supports = array('title'); $labels = array( 'name' => _x('Newss', 'plural'), 'singular_name' => _x('News', 'singular'), 'name_admin_bar' => _x('News', 'admin bar'), 'add_new' => _x('Add New', 'add new'), 'add_new_item' => __('Add New News'), 'new_item' => __('New News'), 'edit_item' => __('Edit News'), 'view_item' => __('View News'), 'all_items' => __('All Newss'), 'search_items' => __('Search News'), 'not_found' => __('No News Found.'), ); $args = array( 'supports' => $supports, 'labels' => $labels, 'public' => true, 'description' => 'News Information', 'has_archive' => true, 'exclude_from_search' => false, 'show_in_menu' => 'edit.php?post_type=news' ); register_post_type('news', $args); }
My Menu code is like below
public function admin_menu() { add_menu_page(‘News Information’, ‘News Info’, ‘manage_options’, ‘news_info’, [$this, ‘api_key_page’], ‘dashicons-info-outline’, 30); add_submenu_page(‘news_info’, ‘News Information’, ‘API Key’, ‘manage_options’, ‘news_info_page’, [$this, ‘api_key_page’]); add_submenu_page(‘news_info’, ‘News Information’, ‘News’, ‘manage_options’, ‘edit.php?post_type=news’); remove_submenu_page(‘news_info’, ‘news_info’); }
I am confused. Is this the working code now based on the tips above? Or is there still a problem? Just by the submenu item “API Key” the menu should already pop up and show 2 menu items. The 2nd entry add_submenu_page() is not even necessary from my point of view, as well as the remove.
Thanks @threadi . Yes, there is still a problem. The submenu item “API Key” is working. But I would like to make the list available as another submenu item of “News Info”.
In register_custom_post_type, change the
'show_menu'
totrue
.Replace the content of admin_menu() with just this line:
'add_submenu_page('edit.php?post_type=news', 'News Information', 'API Key', 'manage_options', 'news_info_page', 'testplugin_api_key_page');
After that you have in the menu:
* News
=> All News (this is the list)
=> New News (to add new entries)
=> API Key (your individual page)Be sure to check out the parameters for https://developer.www.ads-software.com/reference/functions/register_post_type/. You can control the visibility of the custom posttype very precisely. What you have written additionally is not necessary at all.
- The topic ‘Display list of Custom Post’ is closed to new replies.