Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The following code should read: If the current custom post type (bbp_forum), is the one being displayed, assign the class 'current' to its respective <li> tag. But for some reason the class 'current' (to highlight the current bbp_forum link) is being displayed in all the <li> tags:

enter image description here

<body <?php body_class(); ?>>
<div id="wrapper" class="hfeed">
    <div id="header">
        <div id="masthead">
            <div id="branding" role="banner">
                <h1><a href="<?php echo home_url( '/' ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
            </div><!-- #branding -->
            <div id="access" role="navigation">
                <?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
            </div><!-- #access -->
        </div><!-- #masthead -->
        <ul id="forums">
          <?php global $post; $cat_posts = get_posts('post_type=bbp_forum');
          foreach($cat_posts as $post) : ?>
            <li <?php if($post->ID == get_the_ID()){ ?>class="current" <?php } ?>>
                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
             </li>
          <?php endforeach; ?>
        </ul><!-- #access -->
    </div><!-- #header -->

    <div id="main">

Any suggestions?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The expression will always be true. Take a look at get_the_ID();

function get_the_ID() {
    global $post;
    return $post->ID;
}

So your code is effectively running as;

if ( $post->ID == $post->ID ) // always true!

Instead, cache the ID of the main post in a variable, then compare that instead.

<?php

global $post;

/**
 * @var int Current post ID.
 */
$the_post_ID = $post->ID;

/**
 * @var array All posts for bbp_forum.
 */
$cat_posts = get_posts('post_type=bbp_forum');

?>

<?php foreach ( $cat_posts as $post ) : ?>

    <li<?php if ( $post->ID == $the_post_ID ) echo ' class="current"'; ?>>
        <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    </li>

<?php endforeach; ?>
share|improve this answer
    
that worked perfectly thanks. –  janoChen Mar 15 '11 at 4:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.