Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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());
share|improve this answer
    
Thanks Roman. That was exactly what I needed. –  Christopher Sep 25 '11 at 1:48

Your Answer

 
discard

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.