WordPress: How to use post_class() in an echo

The post_class() function just is a wrapper for get_post_class().

Keep in mind that the latter does not echo, but returns an Array so you have to do something like what post_class() does:

$classes = join( '  ', get_post_class() );

Your code (could) would look like the following. I changed the if check as well to first check if $posts is empty and then check if it is an array – which is more fail safe.

if (
    ! empty( $posts )
    AND is_array( $posts )
    )
{
    $items = array();
    foreach ( $posts as $post )
        $items[] = sprintf(
            '<li><div class="overlay"><div class="%s"></div><h3>%s</h3></div></div></li>',
            join( '  ', post_class() ),
            tribe_get_event_taxonomy()
        );

    ! empty( $items ) AND printf(
        '<ul class="tribe-related-events tribe-clearfix hfeed vcalendar">%s</ul>',
        join( '', $items )
    );
}

Leave a Comment