WordPress.org

Ready to get started?Download WordPress

Forums

How to build my menu's HTML different for each page? (2 posts)

  1. Suseika
    Member
    Posted 1 year ago #

    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?

  2. Suseika
    Member
    Posted 1 year ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags