WordPress.org

Support

Support » How-To and Troubleshooting » How to build my menu's HTML different for each page?

How to build my menu's HTML different for each page?

  • On each page of my WordPress blog I have a menu, and one item of that menu must be a link to another page formed using post’s metadata (I store the links in the posts’ custom field). For each post the link would be different, and I am wondering how do I achieve this. What file should I change to build one item of a menu using custom php code?

Viewing 1 replies (of 1 total)
  • I found out the solution and it worked perfectly for me, but I’m not sure that this is the right way. This link helped me a lot: http://www.wpbeginner.com/wp-themes/how-to-add-custom-items-to-specific-wordpress-menus/ I added this code in the template’s header.php before calling wp_nav_menu():

    add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
    function your_custom_menu_item ( $items, $args ) {
        // Here goes anything you wish, it just has to be concatenated with $items variable.
        $items .= '<li><a href="'.get_post_meta(get_the_ID(), "foreignlink", true).'">Read this in Zorblaxian</a></li>';
    
        return $items;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to build my menu's HTML different for each page?’ is closed to new replies.