Viewing 12 replies - 1 through 12 (of 12 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 3 years, 8 months 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 ??

    Hi,

    Did you manage to get it working?

    I am in the same position as you and using Elementor, I would greatly appreciate if you could share your code if of course you were successful ??

    Huge thanks!

    Thread Starter jodybethw

    (@jodybethw)

    No, I have not been able to figure it out. I don’t know why this isn’t easier to implement for non-php coders!

    • This reply was modified 3 years, 5 months ago by jodybethw.

    Hey buddy, I managed to tinker with some code I found online to get this working.

    /* Auto CPT Menu  */
    add_filter( 'wp_nav_menu_objects', 'wpg2byt_add_term_parent');
     function wpg2byt_add_term_parent( $items) {
       $terms = get_terms( array(
         'taxonomy' => 'wpbb_job_type', // enter the reference for your CPT catagory
         'hide_empty' => false,
      ) );
    
       foreach ($terms as $term) {
       //format its data to be compatible with the filter
       $link = array (
             'title'            =>  $term->name,
             'menu_item_parent' => 4533, // get the ID of your menu item with inspect element, this will make the catagories go under this as a submenu
             'ID'               => $term->term_id,
             'db_id'            => '',
             'url'              => get_term_link($term),
             'classes'          => array( 'cpt-auto-populate' ) // include a class to the newly added categories menu items
         );
       $items[] = (object) $link;
       }
       return $items;    
     }

    The only issue is this is being applied to every menu across the website, for now I am hiding the new categories on unwanted menus with the custom class.

    Help from anyone to apply this code to a specific menu would be appreciated.

    Hope this simple solution works for you.

    P.S plugins for this suck. Usually I manage to make anything work with a plugin but this time was forced to work on my php skills which equally suck!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Auto adding new post categories to menu’ is closed to new replies.