The MU forums have moved to WordPress.org

Dynamic Menu Highlighting static links (7 posts)

  1. ciaravino
    Member
    Posted 13 years ago #

    I am trying to get this nav to work, but also be able to distinguish the current page's own link so I can style it differently.

    <li><a href="http://mac.gamingapple.com">Mac</a></li>
    <li><a href="http://ipad.gamingapple.com">iPad</a></li>
    <li><a href="http://iphone.gamingapple.com">iPhone</a></li>

    Each of the links is a static link in my nav that takes you to each of the other blogs. I would know how to do this if it was a normal WordPress installation by using is_home or whatever, but that wouldn't really work here.

    Also, if you know of a built-in MU function that would allow me to dynamically get the same link text, I'd be up for that.

  2. ciaravino
    Member
    Posted 13 years ago #

    I've been trying this:

    <li>
    <?php
    if (get_current_site()->id == 2) {
    echo '<span id="active-nav-link">Mac</span>';
    } else {
    echo '<a href="http://mac.gamingapple.com">Mac</a>';
    }
    ?>
    </li>

    But it doesn't work. Does the code look like it would work for making the first link? I can't get it to work though because all of the return values for get_current_site() only give information on the main blog. For example, get_current_site()->id gives me 1 no matter what blog I'm on. Or, get_current_site()->domain gives me gamingapple.com even if I'm on mac.gamingapple.com.

    Here's some documentation on the function I'm trying to use http://codex.wordpress.org/WPMU_Functions/get_current_site

  3. ciaravino
    Member
    Posted 13 years ago #

    Woot, I figured it out by myself :D (and with some help from the codex).

    get_bloginfo()

    I used get_bloginfo($show) to get the name of the current blog then I made the if statement comparing it to $currentblogname which is set to "Mac Gaming News and Reviews". So, if the output of get_bloginfo($show) is the same as the value of $currentblogname (Mac Gaming News and Reviews), I know I'm on that current page so I put the non-link with the id so I can make that different in the CSS, so people can tell what blog they are on. If I'm currently not on that page, the if statement goes to the else option which is just the normal link.

    <li>
    <?php
    $currentblogname = "Mac Gaming News and Reviews";
    if (get_bloginfo($show) == $currentblogname) {
    	echo '<span id="active-nav-link">Mac</span>';
    } else {
    	echo '<a href="http://mac.gamingapple.com">Mac</a>';
    }
    ?>
    </li>
    <li>
    <?php
    $currentblogname = "iPad Gaming News and Reviews";
    if (get_bloginfo($show) == $currentblogname) {
    	echo '<span id="active-nav-link">iPad</span>';
    } else {
    	echo '<a href="http://ipad.gamingapple.com">iPad</a>';
    }
    ?>
    </li>
    <li>
    <?php
    $currentblogname = "iPhone Gaming News and Reviews";
    if (get_bloginfo($show) == $currentblogname) {
    	echo '<span id="active-nav-link">iPhone</span>';
    } else {
    	echo '<a href="http://iphone.gamingapple.com">iPhone</a>';
    }
    ?>
    </li>

    Hopefully this will help someone if they are looking for the same thing. I'm kinda new to PHP so this could easily be a bad way of accomplishing this.

  4. dsader
    Member
    Posted 13 years ago #

    get_current_site will return info for the main blog of the entire network of blogs.
    What you've arrived at works for you, all well and good.

    But checking the $blog_id is the better way to go, I figure(no editing code just to change a blog title).

    So back to your first example it would become something like this

    <li>
    <?php
    global $blog_id;
    if ( $blog_id == 2 ) {
    echo '<span id="active-nav-link">Mac</span>';
    } else {
    echo '<a href="http://mac.gamingapple.com">Mac</a>';
    }
    ?>
    </li>
  5. ciaravino
    Member
    Posted 13 years ago #

    Don't you have to give $blog_id an integer before you can use it though? I didn't think $blog_id automatically gave you the id of the current blog. If you have to manually give it a value before the if then it will always display the non-link item no matter what page I'm on.

  6. dsader
    Member
    Posted 13 years ago #

    Wordpress has a ton of globals vars already loading depending on the page:

    You'll see what I see if you echo the globals. Paste this into a template:

    echo '<pre>';
    print_r($GLOBALS);
    echo '</pre>';

    Depending on what/where/how you want to ask for a $blog_id may not require the "global $blog_id;". My point is that $blog_id is there somewhere in the globals with every page load already. No need to querie another option from the db.

  7. ciaravino
    Member
    Posted 13 years ago #

    Ah, I see.

About this Topic

  • Started 13 years ago by ciaravino
  • Latest reply from ciaravino