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 am trying to add specific CSS post classes to the services custom post type. However, when I apply this function to the post class filter, it applies the function to all post types. My goal is to change the layout services archive page with CSS classes.

Is the problem with the if ( is_singular('services') ) portion or does it have to do with the global $wp_query; which might be querying every post? Any solution would be greatly appreciated.

The full code:

add_filter( 'post_class', 'woo_custom_add_post_classes', 10);

function woo_custom_add_post_classes ( $classes ) {
    if ( is_singular('services') ) { return $classes; }


    global $wp_query; 

    // Get the number of the current post in the loop.
    $current_count = $wp_query->current_post + 1;

    // Work out whether this post is the last in a row.

    $iflast = 'last';

    if ($current_count % 3 == 0 ) { $iflast = 'last'; } else { $iflast = '';}

    // Add the classes to the array of CSS classes.
    $classes[] = 'service-number-' . $current_count; // Service number. 
    $classes[] = 'threecol-one';
    $classes[] = $iflast; // Last in the row or nothing 

    return $classes;

} // End woo_custom_add_post_classes()
share|improve this question
2  
Is this a typo? if ( is_singular('services') ) { return $classes; } ...I think you mean if ( ! is_singular('services') ) { return $classes; }? –  Chip Bennett Nov 27 '13 at 17:01
    
I guess I should have read more thoroughly.. –  ialocin Nov 27 '13 at 17:02

1 Answer 1

up vote 1 down vote accepted

You can conditionally check for post type like this:

Code:

if ( 'services' == get_post_type( $post->ID ) ) {
    //your code
}

See at the Codex: Conditional Tags, but is_singular() does this too.

share|improve this answer
    
I thought I tried this but apparently not. It worked...thanks! –  itrogers Nov 27 '13 at 17:21
    
Strange, because after I read Chip Bennett's comment, I was convinced he's right. Good thing I didn't deleted the answer. @IanRogers –  ialocin Nov 27 '13 at 18:24

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.