WordPress.org

Ready to get started?Download WordPress

Forums

Body ID in PHP problem (2 posts)

  1. bazzablue
    Member
    Posted 6 years ago #

    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: http://www.snapdesigns.co.uk/index.php

    Any help would be greatly appreciated.

  2. buddha trance
    Member
    Posted 6 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.