Support » Developing with WordPress » Auto adding new post categories to menu
Auto adding new post categories to menu
-
The site is set up with several custom post types: City, State, Foreign, etc. The menu also lists posts by date. Right now, I have to add each new month/year to the menu manually, and if a new category is created (ex., “Florida” under “State”), I have to add “Florida” to the menu manually. I tried to add code using the link below as guide, but it isn’t working. You can see the code, it is live right now. What am I doing wrong? is there an easier way to do this? I tried finding a plugin to handle this and came up empty (https://wordpress.org/plugins/custom-post-type-auto-menu exists but it isn’t working for folks and hasn’t been updated in a year.) THANK YOU!
The page I need help with: [log in to see the link]
-
I see this
wp_nav_menu( array( 'theme_locatio...
printed out to your site. This means you incorrectly placed PHP code into HTML.You should wrap the code in PHP tags:
<?php
before and?>
after the code.There are a number of ways to add menu items dynamically. A custom walker as suggested in your linked SE topic is as good as any.
You can persistently add menu items by code. Menu items are posts of the type “nav_menu_item” and assigned to a menu name as a term in the “nav_menu” taxonomy. Each item has a number of meta values assigned. Study existing data through phpMyAdmin to learn what meta data is required. Be thoughtful about where/how such code is placed. It must only execute once per new item. Most custom WP code executes on every new request.
@kkarpieszuk – I am using Elementor which uses shortcodes to input php into pages, so I put the following code into functions.php, after the walker code:
// Shortcode to output custom PHP in Elementor function wpc_elementor_shortcode( $atts ) { echo "wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new Walker_State_Categories ) );"; } add_shortcode( 'menu_walker', 'wpc_elementor_shortcode');
And then I added this shortcode into Elementor: [menu_walker]
This is how Elementor says to do it. What am I missing?
@bcworkz – that is way beyond my level of understanding php at the moment. I’d love to be able to get the code I have right now working. No luck yet.
I didn’t mean to overwhelm you, I mainly intended to point out there are other approaches. Feel free to ignore. It still might help someone landing here on search.
In what code you do have, don’t quote the wp_nav_menu() call, it will not execute the way you have it. Don’t
echo
either. Shortcode handlers must return all output. WP will do the echo when it’s appropriate. wp_nav_menu() by default will echo the menu. Since it must be collected into a variable for return, include'echo' => false,
in the args array. Verify “primary” location is valid for your theme. While it’s a very common location name, it’s not a required name.Tip: PHP allows a dangling terminal comma in array declarations (only). It’s good to use it because it makes future maintenance easier.
array( 'theme_location' => 'primary', 'walker' => new Walker_State_Categories, 'echo' => false, // terminal comma here OK in array declarations )
If you need to alter the args list later, no need to worry about comma placement if they are always there with every arg.
The heart of your coding effort lies in the Walker_State_Categories class. While the SE example offers a good starting structure, much of it needs alteration to fit your situation and need.
@bcworkz Thank you — I appreciate your help SO much! I made the changes you suggested, but I’m getting a nonce error. I keep going over it but I’m obviously missing something. This is what I’ve used:
// Shortcode to output custom PHP in Elementor function wpc_elementor_shortcode( $atts ) { array( 'theme_location' => 'primary', 'walker' => new Walker_State_Categories, 'echo' => false, ); } add_shortcode( 'menu_walker', 'wpc_elementor_shortcode');
Below is what Elementor says to use for the shortcode; am I calling the array incorrectly inside the function? I can’t seem to find any similar examples online to figure this out on my own.
// Shortcode to output custom PHP in Elementor function wpc_elementor_shortcode( $atts ) { echo "This is my custom PHP output in Elementor!"; } add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');
I’m sorry, I made an assumption about your ability to interpret code fragments that I shouldn’t have. Assuming the walker works correctly, this should do it:
// Shortcode to output custom menu in Elementor function wpc_elementor_shortcode( $atts ) { return wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new Walker_State_Categories, 'echo' => false, )); } add_shortcode( 'menu_walker', 'wpc_elementor_shortcode');
I can likely help unless your need depends a lot on plugin or theme specific code. I’m good with core WP code. I don’t know much of many plugins or themes.
A walker is still on topic here, but you’ll get more eyes on your questions if you start a new topic. There’s half a chance I’ll end up answering anyway 🙂
- You must be logged in to reply to this topic.