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.

I'm trying to add a feature to my loop.php to use with a custom post type. However, I don't want it displayed on all posts, just the ones that are the custom post type (let's say its called "review").

Is there a way to say if post type = review then show this new section, otherwise hide?

I tried using

<?php if( get_post_type() == 'reviews' ) 

but I don't know how to phrase the else statement to just do nothing and continue on normally.

Thank you!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Try the following:

if ( have_posts() )
{
    while( have_posts() )
    {
        the_post();

        if ( 'reviews' === get_post_type( get_the_ID() )
        {
            echo 'I am a post of the post type &rdquo;reviews&ldquo;';

            // We're done here, continue to next post
            continue;
        }

        // Do other stuff
    }
}

EDIT

In case you're not sure if you got that post type available, use the following plugin to check (below your admin user interfaces footer) what custom post types you got available.

<?php
/** Plugin Name: »kaiser« Get custom post type objects */
function wpsedebug_post_type_obj()
{
    if ( ! is_admin() )
        return;

    $pt = get_post_types(
         array(
             'public'   => true
            ,'_builtin' => false
         )
        ,'object'
    );
    return print '<pre>'.var_export( $pt, true ).'</pre>';
}
add_action( 'shutdown', 'wpsedebug_post_type_obj' );
share|improve this answer
    
I tried that, it doesn't display anything if I add that section in. Basically, as soon as I add an if statement to that part of the loop, it blanks out the page. –  Bo Shubinsky Oct 29 '12 at 14:52
    
@BoShubinsky First: White screen means that there's an error and you haven't turned debugging on (see Codex for more info on how to debug in WP). Second: Take a look at the plugin I added to see what your post type really looks like/is named. –  kaiser Oct 29 '12 at 15:32
1  
Got it, thanks! –  Bo Shubinsky Oct 30 '12 at 1:05

try http://codex.wordpress.org/Function_Reference/get_post_type use that to get your custom post type's value and to compare it

For instance:

<?php $myPostType = get_post_type( $post->ID ); 
 if($myPostType == 'Review'){
  //It's a review post type, whoo!
}
?>
share|improve this answer
    
When I try adding that to the loop, it just makes everything blank. It's not displaying any posts at all. –  Bo Shubinsky Oct 29 '12 at 14:44
    
First try <?php echo get_post_type( $post->ID ); ?> and see which value it gives you –  Lucas Cordina Oct 29 '12 at 14:46

I figured it out. I had to format it like this:

<?php if( get_post_type() == 'reviews' ) { ?>
// stuff inside if statement
<?php } ?>
share|improve this answer

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.