post_class()
echos so that’s you’re problem.
From the codex:
If you would prefer to have the post classes returned instead of echoed, you would want to use
get_post_class()
.
So just do this:
$the_post_classes = get_post_class( 'clearfix' );
$the_post_class_string = '';
foreach( $the_post_classes as $post_class ) {
$the_post_class_string .= $post_class . ' ';
}
$defaults = array (
'before' => 'article id="post-' .get_the_ID(). '"' .$the_post_class_string. 'role="main">',
// etc...
UPDATE: Sorry. I forgot this returns an array and not a string. You just need to iterate through the results before spitting them out. Code above is updated and tested.
Related Posts:
- How to add classes to post_class?
- How do I add a custom body class for a specific page ID?
- Add a different class name to each sticky post?
- Remove and add class with body_class() function
- Alternate post_class on each post
- How to use post_class function?
- How to load post_class() locally
- Adding post_class into functions.php query
- Where to use Post class in a real theme, title, content or parent div or article tag?
- How to remove a class from a post_image div?
- How do I create a sticky sidebar?
- How to mark every 3rd post
- How to specify a class added to my gallery
- Child Pages Loop
- Category as Class for Custom Post Type
- WordPress: How to use post_class() in an echo
- Get parent category class in post_class()
- adding a custom css class to post
- Add class to the items in wp_list_pages
- Where should I use post_class()?
- Add filter to wp_list_categories and query what type of taxonomy-terms it use?
- Odd/even class on article tag
- How to add padding between posts
- If Loop has odd number of posts on last page Custom style for last post in it
- How can I make wp_list_categories output li with category-slug as class, for its children?
- Remove classes from post_class()
- Adding body class when post contains a specific shortcode
- WordPress Loop – Style rows of posts differently
- Indirect modification of overloaded property WP_Post::$classes has no effect
- current-menu-item class for custom post type parent
- Duplicate attribute class causing site validation error
- Adding class to featured image
- Theme Check: Could not find post_class
- Add Post Tags to Body Class
- List of Body Classes Generated by body_class()
- Is post_class(); going to slow WordPress page speed
- WordPress post arrangement using post_class
- Add a class to post title if a link ends with a certain extension
- Adding Filter to Homepage only
- How to add class or id to shortcode HTML elements?
- different post-class when template is loaded via ajax
- Output terms to post_class()
- How to make classes for posts?
- add active class based on permalink and url
- Remove Extra Classes from Post Title
- Is it possible to store Custom Post Type data in separate set of tables and still have wp_post class functionality?
- Change Post Class function
- How to assign classes to all elements?
- Post classes in the loop are being changed in certain themes
- Need help “sanitizing” a custom function that pulls category slug into body class
- How to change “post_class()” for a custom post type?
- Add $values to post_class()
- post_class() breaks out of formatting. . . extra markup also generated
- How can i show post_class() within output loop?
- Every second post different class in blog view
- What Generated Classes and IDs That Is Always Used On All Themes?
- post_class not working with css
- Using post_class to style posts indivdually
- How can i remove the posts category class names from body_class
- CSS class on last post in loop ( custom query )
- How to add post classes?
- How to edit the styles for the table of pages/post listed in the admin when you click on pages/posts?
- How can I add a class to particular paragraphs in my post?
- WordPress call post-ID in jquery
- How to style posts selecting a CSS post_class (added through custom_fields?)?
- Add a class to post if it has been recently updated
- Microformats in a single post – layout conflict
- wordpress add field to post_class
- Get categories list with category name in custom WP_List_Table class
- Apply Post Class to Custom Post Type
- How to add class to anchor using wp_nav_menu
- How to get category names in post_class?
- Add a class to post_class if more than one post shares same meta_value_num
- post_class remove tag- or category- from slug
- Enclose title within a div?
- Set class if a meta value is set within post archive
- Correct way adding External classes to Custom Post type and output their methods to template
- adding additional class to get the post thumbnail [duplicate]
- Add all category as classes in foreach loop
- How do I use $wp_query->current_post with get_template_part? I’m trying to add post classes (first/last/even/odd) to custom loops
- get_post_class not working properly
- Highlight a Post on archive page if it has a new comment?
- Custom query with post_class filter using current_post not working
- Method ‘post_title’ not found in class.
- Post Class for Custom Taxonomy Error
- post_type_exists keeps returning false
- Different styles for categories – need to edit a plugin
- WordPress Tags in class
- Help with output of post classes using apply_filters
- How to mark every 3rd post
- PHP Use Declared array Variable inside already Declared Array