WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Change class="" based on page (5 posts)

  1. daddymccoy
    Member
    Posted 2 years ago #

    I am setting my navigation elements to change it's class to class="selected" when you are on a specific page in the site. I have it working, except for the blog (home.php) page. I have set the "posts page" to "blog" in the Reading Settings, and here is the code that tells each page (or child of a page) which navigational section to change the class for

    //* WORK *//
    $work_selected = '';
    if (is_page('work') || $post->post_parent == '39') {
      $work_selected = 'selected';
    } else {
      $work_selected = '';
    }
    //* BLOG *//
    $blog_selected = '';
    if (is_page('blog') || $post->post_parent == '64') {
      $blog_selected = 'selected';
    } else {
      $blog_selected = '';

    Again, the WORK version does indeed work, as does the other pages on the site. For some reason, the BLOG page does not work. Any help?

  2. Andrew Bartel
    Member
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/is_page

    Likely blog is not the slug or title of the page you think it is. Go to the page in wp admin and figure out what the slug is. Ideally, you would use ids though.

  3. daddymccoy
    Member
    Posted 2 years ago #

    That is what I thought to. I went back and found the page ID (64) and replaced the code to the following, and still no luck.

    //* BLOG *//
    $blog_selected = '';
    if (is_page('64')) {
      $blog_selected = ' selected';
    } else {
      $blog_selected = '';
    }
  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    the posts page ('blog' page) reacts on is_home()

    http://codex.wordpress.org/Conditional_Tags#The_Main_Page

  5. daddymccoy
    Member
    Posted 2 years ago #

    That worked. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic