WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

share|improve this question
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.


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

Your Answer


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.