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>

echo imageHolder(2);
| improve this question | | | | |
function imageHolder($id){
    $category  = get_the_category($id);
    $class = '"category-' . strtolower($category[0]->cat_name) . '"';
    print <<<HTML
    <div class=$class>
       This is a Test

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

| improve this answer | | | | |
  • is there a reason for you to change return <<< to print <<< in some way of best practice methods? Thanks for this great work-around! – Marian Rick Sep 12 '16 at 13:14
  • Hi Marian, the return <<< is also gonna works. But you will need to echo your function. They are pretty much the same, in my opinion. Therefore there is no specific reason why did I change the return <<< to print <<<. I am glad I helped you out with this. – Siyu Qian Sep 12 '16 at 21:33

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.