0

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()
2
  • 2
    Is this a typo? if ( is_singular('services') ) { return $classes; } ...I think you mean if ( ! is_singular('services') ) { return $classes; }? Nov 27, 2013 at 17:01
  • I guess I should have read more thoroughly.. Nov 27, 2013 at 17:02

1 Answer 1

1

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.

2
  • I thought I tried this but apparently not. It worked...thanks!
    – itrogers
    Nov 27, 2013 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 Nov 27, 2013 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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