Question

I can use post_class() to echo all classes for a post. How can I output only the category-ID as a class, and how can I include this while using heredoc string syntax?

I got this function in which I would like to use post_class(), and I have tried get_post_class() which does not work, because its an array to string conversion.

function imageHolder($ID) {
    $classes = get_post_class($ID, 'image-holder');
    return <<<HTML
        <div $classes></div>
HTML;
}

echo imageHolder(2);
Was it helpful?

Solution

function imageHolder($id){
    $category  = get_the_category($id);
    $class = '"category-' . strtolower($category[0]->cat_name) . '"';
    print <<<HTML
    <div class=$class>
       This is a Test
    </div>
    HTML;
}
imageHolder('1');

Please be careful about using '/n' or '/r' symbol in the Heredoc syntax(they might cause the function does not work properly).

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top