Obvious newbie question I'm sure. Working on a friend's site and trying to figure out how to add post_class to a query. Just adding works fine and adds the div, but I don't know how to get it to run the "id="post-<?php the_ID(); ?>" <?php post_class(); ?>" part of it instead of echoing it.

$sidebar .= sprintf('<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>><p><span class="CityCountry">%s</span>' . chr(13),

I'm sure it's a formatting thing of some sort that I don't know yet... still so much to learn...

link|improve this question

62% accept rate
feedback

1 Answer

up vote 2 down vote accepted
  • post_class() will echo the class in the format class="postclassA postclassB"
  • get_post_class() will return an array of all registred classes

To use get_post_class() inside sprintf(), you have to return the array as a string. For this purpose you can use the join() function.

Example

$classes = join(' ', get_post_class())
$sidebar = sprintf('<div id="post-%s" class="%s">%s</div>', get_the_ID(), $classes, get_the_content());
link|improve this answer
Thanks Roman. That was exactly what I needed. – Christopher Sep 25 '11 at 1:48
feedback

Your Answer

 
or
required, but never shown

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