• Hi! I’m trying to set up my header code to dynamically highlight the current page in the menu. To do this, I’m trying to assign a “currentpage” class to the menu item representing the current page or category, using “in_category” and “is_page”. But they seem to be acting inconsistently.

    I’ve set up this code at the top of my header.php:

    <?php if ( in_category('1') ) {
    		$thisPage="blog";
    } elseif ( in_category('3') ) {
    		$thisPage="library";
    } elseif ( is_page('3') ) {
    		$thisPage="storetheatre";
    } elseif ( in_category('4') ) {
    		$thisPage="events";
    } elseif ( is_page('5') ) {
    		$thisPage="flamenco";
    } elseif ( is_page('14') || in_category('10') ) {
    		$thisPage="aboutus";
    } elseif ( is_page('8') ) {
    		$thisPage="contact";
    } ?>

    And added code to the menu along the lines of this:

    <li<?php if ($thisPage=="storetheatre") {echo " id=\"currentpage\""; }?>><a href="http://ediehats.com/wordpress/?page_id=3">Store/Theatre</a>

    But the “currentpage” code is being inconsistently applied. When I load a page and view the source, the “id=currentpage” gets applied only on these categories: Blog, Library, Events. In other words, only the category-based logic works. But for the page-based ones – Store/Theatre, Flamenco, About Us and Contact – it doesn’t work. Instead, and strangely, the first menu item, “blog”, has “currentpage” applied to it.

    I was sure I was using the is_page syntax correctly but it seems not to be working here… and I can’t imagine why!

    Hope someone can point me in the right direction.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I see your logic there, and it should work, but I think that’s a very odd way of doing current page stuff.

    The way most people do it (and the way that wp_list_pages does it) is to get the current page number and then compare it against whatever you are outputting. This tends to lend itself well when you build the list of pages dynamically instead of manually hard-coding it.

    Have you looked at the wp_list_pages() and wp_list_categories() functions instead? These generate your list of pages/categories for you, and with some styling, you can make them show up the way you want.

    I don’t know if this helps, but here’s a fragment from my sidebar code:

    if (is_home()) {
        echo '<li><strong>Home Page</strong></li>';
      } else {
        echo '<li><a href="/" title="Home Page">Home Page</a></li>';
      }
    
      if (is_page('page-slug')) { // Example only
        echo '<li><strong>Page Title</strong></li>';
      } else {
        echo '<li><a href="/page-slug" title="Page Title">Page Title</a></li>';
      }
    
      // PICTURE GALLERY
    
      if (is_single()) {
        // We're looking at a single post
        // Get the current post's ID using the global $wp_query variable
        $id = $wp_query->post->ID;
        if (($id == ('128')) || ($id == ('78')) || ($id == ('70')) || ($id == ('76')) || ($id == ('71')) || ($id == ('72')) || ($id == ('81')) || ($id == ('82')) || ($id == ('84')) || ($id == ('88')) || ($id == ('100')) || ($id == ('104')) || ($id == ('105')) || ($id == ('108')) || ($id == ('114')) || ($id == ('123'))) {
          $pictures = true;
        }
      } else {
        $pictures = false;
      }
    
      if ($pictures == false) { // If this is not a picture page, print a normal link to first picture page:
        echo '<li><a href="/128" title="Picture Gallery">Picture Gallery</a></li>';
      } else if ($pictures == true) { // If this is a picture page, no link and use Suckerfish:
        echo '<li class="level3"><strong>More Pictures</strong>';
        include (TEMPLATEPATH . "/sublist-pg.php");
        echo '</li>';
      }

    Some of the logic might be helpful if you’re wanting to convert links to non-links when viewing a particular Page or post.

    Not sure if this is any help, but I wonder if you’re approaching this from a slightly odd angle? Would it not help to use conditionals as above, but apply them to unique body classes? This way you could then style your list using CSS, based on what the current body class is.

    Make sense?

    Another way might be to add the following to your head:

    <?php
    $page = $_SERVER[‘REQUEST_URI’];
    $page = str_replace(“/”,””,$page);
    $page = str_replace(“.php”,””,$page);
    $page = str_replace(“?s=”,””,$page);
    $page = $page ? $page : ‘default’
    ?>

    Then echo the page string in your body, as an id or class. This, however, gives a unique id or class, rather than a consistent one for things such as categories. I think the conditional statements work well for that end.

    Hope this is in some way useful, I’m only a beginner on all this stuff but have come across some similar problems…

    J

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘logic weirdness with is_page and is_category’ is closed to new replies.