WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlight parent page that lists custom post type (7 posts)

  1. khyams
    Member
    Posted 2 years ago #

    I'm using this string of php (along with some css) to highlight the current page of my website:

    <ul>
       <li><a href="/portfolio" <?php if (is_page('portfolio'))
       {echo " id=\"current\"";} ?>>Portfolio</a></li>
       <li><a href="/about" <?php if (is_page('about'))
       {echo " id=\"current\"";} ?>>About</a></li>
       <li><a href="/blog" <?php if (is_page('blog'))
       {echo " id=\"current\"";} ?>>Blog</a></li>
    </ul>

    At the moment it works, but my portfolio page is populated by a custom post type (in theory that would make it a parent page). When I select a portfolio entry (child page), I would like the link for the parent page to remain highlighted in the navigation. Is this possible and if so, then how?

    I'm using a customized version of the Starkers theme.

  2. SpankMarvin
    Member
    Posted 2 years ago #

    Top of my head, not tested:

    <?php
    if( is_singular() ){
      $p_type = get_post_type( $post->ID );
    }
    ?>

    Then you could replace your Portfolio menu list item with:

    <li><a href="/portfolio" <?php if ( is_page('portfolio') || ( $p_type == 'portfolio' ) )
       {echo " id=\"current\"";} ?>>Portfolio</a></li>

    Another small thing: I would recommend that rather than assign an id you add to the menu item's class. Safer bet against duplicate ids somewhere along the line.

  3. khyams
    Member
    Posted 2 years ago #

    I'll give that a try later today. Just to clarify, you put this string of code within the head tags...

    <?php
    if( is_singular() ){
      $p_type = get_post_type( $post->ID );
    }
    ?>

    Thanks for your help.

  4. SpankMarvin
    Member
    Posted 2 years ago #

    Place that code somewhere before the menu code. And remember to include the extra code above in your "if" conditional, otherwise there will be no difference in the menu's output.

  5. SpankMarvin
    Member
    Posted 2 years ago #

    Oh, BTW, I am assuming that your post type slug is 'portfolio', which it might not be. Basically $p_type will be whatever custom post type name you assigned, so you'll need to consider that in the "if" conditional you have set, so it's comparing $p_type with the custom post type name rather than the portfolio page slug.

  6. khyams
    Member
    Posted 2 years ago #

    Yeah, my post type slug is 'portfolio'.

  7. khyams
    Member
    Posted 2 years ago #

    So I finally got around to plugging those snippets in, and it works like a charm. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic