• Resolved liquilife

    (@liquilife)


    Good afternoon,

    I have been trying to work with the dynamic menu highlighting at https://codex.www.ads-software.com/Dynamic_Menu_Highlighting . I’ve run into a huge issue with the tutorial that renderes it quite useless unless there is a solution. There is no way to question if a post is under a category. I’ve got tabs for 3 categories and when viewing any post it seems impossible to query it using the “is” statements based upon the category it is in, therefore rendering the dyanamic highlighting menu non-functional once you get past the category archive.

    This is what I’ve got so far:
    <?php
    if ( is_home() ) { $current = 'one'; }
    elseif ( is_category('News') ) { $current = 'two'; }
    elseif ( is_single('news') ) { $current = 'two'; }
    elseif ( is_category('1') ) { $current = 'three'; }
    elseif ( is_category('3') ) { $current = 'four'; }
    elseif ( is_page('gallery') ) { $current = 'five'; }
    elseif ( is_page('junctions') ) { $current = 'six'; }
    elseif ( is_page('contact') ) { $current = 'seven'; }
    ?>

    I’ve read through all the “is”conditional tags at https://codex.www.ads-software.com/Conditional_Tags and there just does not seem to be one to identify a post by it’s category. Can any one help me? Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Since your menu is most likely displayed before The Loop, you’ll need to use a bit of trickery.

    get_the_category() can be used to fetch a single post’s categories. It’s supposed to be called in The Loop. but you can get around that like this:

    <?php
    if (is_single()) {
    // we're looking at a single post
    $this_id = $wp_query->post->ID;
    $cats = get_the_category($this_id);
    } // done with the is_single check

    Then you can use something like the following for $cats :
    <?php
    foreach ($cats as $cat) {
    if (2 == $cat->cat_ID) {
    // highlight the proper menu item
    } elseif (3 == $cat->cat_ID) {
    // highlight the proper menu item
    } // end if
    } // end foreach

    Let us know if that helps you!

    Thread Starter liquilife

    (@liquilife)

    It looks great, but not being a php novice I’m unsure of how that would correctly integrate into my existing php conditional statements. Can you give me a small example of how it would integrate with my code?

    Thanks for your help thus far, it is much much appreciated ?? Everyday I work with wordpress I pick up a new bit of knowledge with PHP I feel like.

    Robbiw

    Thread Starter liquilife

    (@liquilife)

    Ok, it is actually working! Getting close, I can view a post and highlight the menu item, the only problem is when I am viewing a category, archive, custom page or home page I get the following error at the top of my site:
    Warning: Invalid argument supplied for foreach() in /home/.haman/liquilife/tonicnews.com/go/wp-content/themes/tonicnews/header.php on line 29
    The page renderes fine and the dynamic menu highlighting still works, just that ugly error message. This is what I’ve got so far:
    <?php
    if (is_single()) {
    $this_id = $wp_query->post->ID;
    $cats = get_the_category($this_id);
    } ?>
    <?php
    foreach ($cats as $cat) {
    if (5 == $cat->cat_ID) { $current = 'two'; }
    elseif (1 == $cat->cat_ID) { $current = 'three'; }
    elseif (3 == $cat->cat_ID) { $current = 'four'; }
    } ?>
    <?php
    if ( is_home() ) { $current = 'one'; }
    elseif ( is_category('5') ) { $current = 'two'; }
    elseif ( is_category('1') ) { $current = 'three'; }
    elseif ( is_category('3') ) { $current = 'four'; }
    elseif ( is_page('gallery') ) { $current = 'five'; }
    elseif ( is_page('junctions') ) { $current = 'six'; }
    elseif ( is_page('contact') ) { $current = 'seven'; }
    ?>
    <style type="text/css">
    #mainnav li a#<?php echo $current; ?> {
    background: #000000;
    color:#FFFFFF;
    }
    </style>

    The fix is simple: make sure the foreach loop occurs inside the if (is_single()) block, like this:
    <?php
    if (is_single()) {
    $this_id = $wp_query->post->ID;
    $cats = get_the_category($this_id);
    foreach ($cats as $cat) {
    if (5 == $cat->cat_ID) { $current = 'two'; }
    elseif (1 == $cat->cat_ID) { $current = 'three'; }
    elseif (3 == $cat->cat_ID) { $current = 'four'; }
    } // end foreach
    } //end if (is_single())
    ?>

    Thread Starter liquilife

    (@liquilife)

    Awesome, thank you Skippy for your help, it is much appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘# is_page() and other conditional statements’ is closed to new replies.