0

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?

1
  • Why are you parsing args, when you're not doing any input? Normally the wp_parse_args() function should throw an error.
    – kaiser
    Jul 26 '12 at 20:01
3

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.

2
  • 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
2

A bit shorter and easier:

implode (' ', get_post_class( 'clearfix' ) )

This just makes it shorter and allows you to be rid of the pesky space at the end of the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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