AI Verified

Name

Autopopulate menu item with posts by post type

About

Autopopulate menu item with posts by post type as submenu items

Language

PHP

Rating

Voted: 3 by 3 user(s)

How to Setup Snippet

Install, define the $post_type variable, add CSS Class to the parent menu item, enjoy!

Codevault

hoofer

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

28/07/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Autopopulate menu item with posts by post type

 
                    
1add_filter( 'wp_get_nav_menu_items', function( $items, $menu, $args ) {
2 $child_items = array(); // here, we will add all items for the single posts
3 $menu_order = count($items); // this is required, to make sure it doesn't push out other menu items
4 $parent_item_id = 0; // we will use this variable to identify the parent menu item
5 $post_type = 'custom_post_type'; // we will use this variable to define the post type to display
6 
7 //First, we loop through all menu items to find the one we want to be the parent of the sub-menu with all the posts.
8 foreach ( $items as $item ) {
9 if ( in_array("$post_type-parent", $item->classes) ){ // add this class to the parent menu item. current ex: "custom_post_type-parent"
10 $parent_item_id = $item->ID;
11 }
12 }
13 
14 if($parent_item_id > 0){
15 
16 foreach ( get_posts( "post_type=$post_type&numberposts=-1" ) as $post ) {
17 $post->menu_item_parent = $parent_item_id;
18 $post->post_type = 'nav_menu_item';
19 $post->object = 'custom';
20 $post->type = 'custom';
21 $post->menu_order = ++$menu_order;
22 $post->title = $post->post_title;
23 $post->url = get_relative_permalink( $post->ID );
24 array_push($child_items, $post);
25 }
26 
27 }
28 
29 return array_merge( $items, $child_items );
30}, 10, 3 );

3

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

1

Make upload filenames lowercase

Added: 1 year ago

Last Updated: 1 year ago

Ensures that the filenames of all uploaded files are converted to lowercase, for consistency.

General

Unverified

0

WordPress last updated function

Added: 1 year ago

Last Updated: 1 year ago

This is a block of PHP code that can be used to display the date and time when a WordPress post or page was last updated. Here's how it works:

General

AI Verified

0

Get Featured Image Metadata for Current Post

Added: 1 year ago

Last Updated: 1 year ago

This creates a shortcode that returns the requested metadata item for the current posts' featured image. The item values that can be returned are alt, title, caption, description, or url.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

3

Autopopulate menu item with posts by post type

Added: 1 year ago

Last Updated: 11 months ago

Autopopulate menu item with posts by post type as submenu items

WordPress Admin

AI Verified

2

Disable admin notices by text string

Added: 1 year ago

Last Updated: 1 year ago

In $forbidden_message_strings array, provide a list of keywords or phrases that you want to disable. Provide a longer phrase to increase specificity

WordPress Admin

AI Verified

1

Save in WP Admin with CTRL + S

Added: 1 year ago

Last Updated: 1 month ago

Save time by hitting CTRL/CMD + S instead of scrolling to find the Save/Update/Publish button