• Just producing my first site in WordPress and am a complete newbie to PHP. I want the nav bar to show an active state identifying the page being viewed. After trying a few different pieces of code (most of which didn’t work at all) I came across this one which almost works:

    <?php
    $current_page = $post->ID;
    $parent = 1;
    while($parent) {
    $page_query = $wpdb->get_row(“SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = ‘$current_page'”);
    $parent = $current_page = $page_query->post_parent;
    if(!$parent)
    $parent_name = $page_query->post_name;
    }
    ?>

    <body id=”<?php echo (is_page()) ? “$parent_name” : ((is_home()) ? “blog” : ((is_search()) ? “other” : ((is_single()) ? “blog” : “blog”))); ?>”>

    My main menu has six options and this code works on four of them but not the other two (portfolio and contact). The site can be viewed here: https://www.snapdesigns.co.uk/index.php

    Any help would be greatly appreciated.

Viewing 1 replies (of 1 total)
  • bazzablue,

    Maybe this can help you. Because I have the same active page feature, you can try this way:

    In header.php of your theme, there is only a “list pages” function

    <ul>
    <?php wp_list_pages('title_li='); ?>
    		</ul>

    You can try my code, which lists a “current page item”

    <li class="<?php if ( is_home() or is_archive() or is_single() or is_paged() or is_search() or (function_exists('is_tag') and is_tag()) )
    	{ ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a title="<?php bloginfo('name'); ?>" href="<?php bloginfo('url'); ?>">Home</a></li>
    
    	<?php wp_list_pages('depth=1&title_li=&exclude='); ?>

    Then create a css class for the a:hover. I have

    ul li.current_page_item a:hover {
    color:#your color;
    }
    
    ul li.current_page_item a:hover {
    background:#your color;
    }

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Body ID in PHP problem’ is closed to new replies.