I'm using a plugin to add recent posts to home page and I am using different styles for the categories. I have the different styles working on the blog page but not on the posts output from the plugin.

This is the code I need to add:

<?php post_class(); ?> 

And I think this is where it should go but can't figure out how to do that:

// Opening single post container.
$srp_content .= '<div id="' . $srp_post_id . '" class="srp-widget-singlepost ' . $single_post_additional_classes . '">';
    // Checking if "post title above thumb" option is on.
    if ( 'yes' == $this->widget_args[ 'post_title_above_thumb' ] ) {
        // Generating the post title.
        $srp_content .= $this->generate_post_title( $recent_posts->post );

1 Answer 1


If I'm not totally missing some thing this should be achievable with a minor change to your code. You can not use post_class() in your code as it outputs/echos the classes. But there is get_post_class(), which does return the classes, but they are returned as a array. So you need to make a string out of it, to make use in your code. Lets do this just the way it is done for post_class() - see source - with PHP's join(), but without echoing and adding the attribute itself.

Generally it would look like this:

$post_classes_string = join( ' ', get_post_class( $class, $post_id ) );

If you don't need an extra class replace $class with ''. If you are in The Loop you can omit the $post_id, otherwise you will need it. The returned post classes contain some that look like category-* where the * will be the category name, which gives you every possibility to address your posts on category basis.

Last but not least, after you figured out above mentioned details, you should be able to simply add the classes by doing:

$srp_content .= '<div 
    id="' . $srp_post_id . '" 
         ' . $single_post_additional_classes . ' ' . $post_classes_string . '">';
  • thanks for the help but the last bit of code didn't work so I added in the first part and that changed the output to add post-4155 page type-page status-publish hentry but didn't add the category. Commented Nov 21, 2014 at 14:29
  • Those are likely the classes from the page you are on, not the ones from the single posts. Then you have to make use of the $post_id of the single post, which I suspect is available already because of $srp_post_id (not to say this is it), but can't actually tell from the code in your question. @Geraldine Commented Nov 21, 2014 at 14:42
  • You were right that was the page classes. Your edited code doesn't add any extra classes. Thanks so much for trying to help. Commented Nov 21, 2014 at 14:57
  • Too bad, but I can tell you that much, the code would work, I'm certain of that, if you were actually are getting extra classes. So this is what you have to figure out. @Geraldine Commented Nov 21, 2014 at 15:09

Your Answer

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

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