Support » Developing with WordPress » Auto adding new post categories to menu

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter jodybethw

    (@jodybethw)

    @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?

    Thread Starter jodybethw

    (@jodybethw)

    @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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter jodybethw

    (@jodybethw)

    @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');
    Moderator bcworkz

    (@bcworkz)

    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');
    Thread Starter jodybethw

    (@jodybethw)

    Thank you, @bcworkz! I now have to work on the walker. Is that something that you are also able to help with, or shall I open a new thread if I get stuck? Will try to do what I can on my own, of course.

    • This reply was modified 1 month, 1 week ago by jodybethw.
    Moderator bcworkz

    (@bcworkz)

    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 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.