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.

Trying this in my functions file. There is actually a lot more to this function than I am showing, but this is the important part that is messed up.


function xyz_loop() {
        $defaults = array (
            'before' => 'article id="post-' .get_the_ID(). '"' .post_class('clearfix'). 'role="main">',
            'after' => '/article>',
        );

$args = wp_parse_args( $args, $defaults ); extract( $args, EXTR_SKIP ); echo $before; echo 'doing stuff here'; echo $after;}

the problem is the post_class is outputting the post classes before the article container instead of inside it.

Can I not use post_class like this?

share|improve this question
    
Why are you parsing args, when you're not doing any input? Normally the wp_pars_args function should throw an error. –  kaiser Jul 26 '12 at 20:01
add comment

1 Answer

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.

share|improve this answer
    
that is producing article id="post-165" arrayrole="main"> –  mike Jul 27 '12 at 7:28
    
do you know why it is outputting wrong? (arrayrole="main"> ? –  mike Jul 27 '12 at 19:04
add comment

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.