WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic or variable home link in shared primary nav - MU (7 posts)

  1. waitek
    Member
    Posted 1 year ago #

    I'm using a single primary nav across a network of sites by switching the blog id before calling for the menu--then switching back to the initial blog id for everything else.

    However, I need one more refinement. For subsites, the home link in the primary nav needs to point to the current blog--not to the root website, which is where the primary nav is coming from.

    Here's the live example:

    Root website--www.adoptaworker.org
    Here the home button on the primary nav needs to point to http://www.adoptaworker.org, which it currently does--so no problem here.

    Subsite--www.adoptaworker.org/356-00125/
    With this adopted worker subsite, the home button on the primary nav needs to point to http://www.adoptaworker.org/356-00125/, but everything else in the primary nav would continue to come from the shared network nav from the initial blog id.

    Here's the code I'm using to share the primary nav across all the subsites:

    //store the current blog_id being viewed
    global $blog_id;
    $current_blog_id = $blog_id;
    
    //switch to the main blog which will have an id of 1
    switch_to_blog(1);
    
    //output the WordPress navigation menu
    wp_nav_menu(
       //add your arguments here
    );
    
    //switch back to the current blog being viewed
    switch_to_blog($current_blog_id);

    Thanks in advance for any suggestions.

  2. Why not use get_site_url($current_blog_id);

    You're already defining that so you should be able to call it :)

  3. waitek
    Member
    Posted 1 year ago #

    OK. Haven't been under the hood for nav stuff--just relied on WordPress to spit it out. How or where would I tweak the code to affect just the home link?

  4. You'd want to attack wp_nav_menu()

    Extending the example in 'Adding a word at the begining of the menu'
    http://codex.wordpress.org/Function_Reference/wp_nav_menu#Adding_a_Word_at_the_Beginning_of_the_Menu

    $current_url = get_site_url($current_blog_id);
    wp_nav_menu( array( 'theme_location' => 'primary', 'items_wrap' => '<ul><li id="item-id"><a href="$current_url">Home</a></li>%3$s</ul>' ) );

    That's a guess. The other idea would be to filter it:

    function new_nav_menu_items($items) {
        $homelink = '<li class="home"><a href="' . get_site_url($current_blog_id) . '">' . __('Home') . '</a></li>';
        $items = $homelink . $items;
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
    
    //output the WordPress navigation menu
    wp_nav_menu(
       //add your arguments here
    );
  5. You'd want to attack wp_nav_menu()

    Extending the example in 'Adding a word at the begining of the menu'
    http://codex.wordpress.org/Function_Reference/wp_nav_menu#Adding_a_Word_at_the_Beginning_of_the_Menu

    $current_url = get_site_url($current_blog_id);
    wp_nav_menu( array( 'theme_location' => 'primary', 'items_wrap' => '<ul><li id="item-id"><a href="$current_url">Home</a></li>%3$s</ul>' ) );

    That's a guess. The other idea would be to filter it:

    function new_nav_menu_items($items) {
        $homelink = '<li class="home"><a href="' . get_site_url($current_blog_id) . '">' . __('Home') . '</a></li>';
        $items = $homelink . $items;
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
    
    //output the WordPress navigation menu
    wp_nav_menu(
       //add your arguments here
    );
  6. waitek
    Member
    Posted 1 year ago #

    Thank you. I'll dig into this.

  7. waitek
    Member
    Posted 1 year ago #

    I've decided to use PHP sessions to hang onto the initial home link variable. Can't seem to maintain that variable otherwise.

    So I'm initializing the session in the functions.php file.

    if ( !session_id() )
    add_action( 'init', 'session_start' );

    Tried setting the home page variable there too, but no luck. So I've coded that into the header--so it checks to see if the site visitor is entering on a subsite that contains a numeric id with this pattern 000-00000. If so, a home page variable is set accordingly:

    if (!$_SESSION['aawid']) {
       $current_url = get_site_url($current_blog_id);
       $aaw_id_pattern = '/[0-9]{3}\-[0-9]{5}/';
       preg_match($aaw_id_pattern, $current_url, $aaw_id);
       $_SESSION['aawid'] = $aaw_id[0];
       $_SESSION['aawhome'] = "adoptaworker.org/".$_SESSION['aawid'];
    }

    The above works great. The issue I'm having is using that variable as the home page link in the menu.

    When I use the following code, the home menu item is appending my desired link at the end of the URL for the current page:

    $aawhome = 'http://adoptaworker.org';
    if ($_SESSION['aawhome']) $aawhome = $_SESSION['aawhome'];
    wp_nav_menu( array( 'theme_location' => 'primary', 'items_wrap' => '<ul><li id="item-id"><a href="'.$aawhome.'">Home</a></li>%3$s</ul>' ) );

    So, instead of adoptaworker.org/000-00000, I'm getting the current page URL *plus* adoptaworker.org/000-00000.

    I've looked through the codex info on WP menus, but I haven't figured out how to override WP or force it to use just the variable I'm providing. Are the WP menu links in an array? I'm probably overlooking something really obvious here :/.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.