WordPress.org

Ready to get started?Download WordPress

Forums

Create an id="current" on the blog page (7 posts)

  1. blicht454
    Member
    Posted 3 years ago #

    I'm trying to create an id="current" for all of my menu items so that the user can see what page they're on. It's working for every page, using the following code:

    <?php if (is_page('Home')) {
    echo " id=\"current\"";
    }?>>

    The only problem is, for the page where I have my posts coming in (the blog page), this code doesn't work, because the blog page is not actually being recognized as a page. (I've just set my posts to appear there via the Reading Settings / Front page displays.)

    Any help would be appreciated!
    Thanks!

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    if the blog page is set as the front page, try:

    <?php if (is_front_page()) {
    echo " id=\"current\"";
    }?>>

    http://codex.wordpress.org/Conditional_Tags

  3. JarretC
    Member
    Posted 3 years ago #

    Depending upon how you are coding you can also use wp_nav_menu (http://codex.wordpress.org/Function_Reference/wp_nav_menu) which sets a class of current_page_item for you automatically.

  4. blicht454
    Member
    Posted 3 years ago #

    The blog page is not set as the front page, it's set as the "posts" page. Any ideas?

    I'm not using the wp_nav_menu, although I suppose I could, if there's no other way to make this work. Here's a link to the site, if it helps: blichttesting.com/one

    You can see that right now the 'blog' is always current, since I haven't figured it out yet.

    Another question if you have the time: is there a way to make the id=current valid for more than one page?

    <?php if (is_page('Home')) {
    echo " id=\"current\"";
    }?>>

    For example, in the code above, could I make more than just 'Home' current?

  5. blicht454
    Member
    Posted 3 years ago #

    Ah, forget the second part of my question, figured it out using an array. And for the first part, if I set up the code like this:

    <?php if (is_page()){
    echo " id=\"current\"";
    }?>>

    Then the blog page is always current, unless I'm on the blog page...

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    after looking into the code of 'body_class()' (in /wp-includes/post-template.php) which generates the .blog css class for the blog page from this conditional statement:

    if ( is_home() )
             $classes[] = 'blog';

    - i would try:

    <?php if (is_home()){
    echo " id=\"current\"";
    }?>>
  7. blicht454
    Member
    Posted 3 years ago #

    Amazing! You're a legend, thanks. That works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic