• Resolved khyams

    (@khyams)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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.

    Thread Starter khyams

    (@khyams)

    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.

    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.

    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.

    Thread Starter khyams

    (@khyams)

    Yeah, my post type slug is ‘portfolio’.

    Thread Starter khyams

    (@khyams)

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Highlight parent page that lists custom post type’ is closed to new replies.