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.

Basically, I would like to activate a CSS class only if the current page is a certain custom post type (e.g. ?bbp_forum=technology) or if the current page is a child of that custom post type.

EDIT:

The first custom post type is called 'bbp_forum.' Each 'bbp_forum' can have a 'bbp_topic' (which is another custom post type).

Like it is described in this picture:

enter image description here

I used to do the following in order to assign the class current to the current 'bbp_forum's link:

        <?php
            global $post;
            $the_post_ID = $post->ID;
            $cat_posts = get_posts('post_type=bbp_forum&posts_per_page=-1');
            var_dump($the_post_ID);
        ?>
        <?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; ?>

but it doesn't work for a child of the 'bbp_forum'

share|improve this question
    
Why don't you explain the use-cases for your custom post type and especially for the child post types. Are they the same types as their parents, or different? –  MikeSchinkel Apr 5 '11 at 10:46
    
@MikeSchinkel OK I updated my question please see my EDIT. –  janoChen Apr 5 '11 at 10:58

1 Answer 1

up vote 0 down vote accepted

I accidentally got the answer:

        <?php
            global $post;
            $the_post_parent = $post->post_parent;
            $the_post_ID = $post->ID;
            $cat_posts = get_posts('post_type=bbp_forum&posts_per_page=-1');
            print_r($post_parent);
        ?>
        <?php foreach ( $cat_posts as $post ) : ?>
            <li <?php if ( $post->ID == $the_post_ID || $post->ID == $the_post_parent ) 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; ?>

I would appreciate if someone explain it to me.

share|improve this answer
    
its just the same as you did before but instead of just checking if it matches the current post, it check if it matches the current posts parent. –  Bainternet Apr 5 '11 at 14:07
    
@Bainternet yeah I figured out later. I'm a bit slow. Thanks man. –  janoChen Apr 5 '11 at 16:58

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.