Eval string for wordpress "in_category" workaround outside the loop

Posted by fimbim on Stack Overflow See other posts from Stack Overflow or by fimbim
Published on 2012-11-17T19:29:14Z Indexed on 2012/11/18 11:00 UTC
Read the original article Hit count: 135

Filed under:
|
|
|

In Wordpress you can´t use "in_category" outside the loop, so I created a function that gives me all the categories my article is in and create a "is_category" if statement out of it.

I put my function in my "functions.php":

function inCatAlt($catID){
    $allCats = get_categories('child_of='.$catID);

    $childCats = 'is_category('.$catID.') ';
    foreach($allCats as $childCat){
        $childCats.= 'or is_category('.$childCat->cat_ID.') ';
    };

    $allchildCats = trim(trim($childCats, 'or '));
    return $allchildCats;
}

and call this in my sidebar, single and so on:

echo inCatAlt(13);

which gives me this as a string back:

"is_category(13) or is_category(16) or is_category(15)"

This is exactly what I needed, but now I want to evaluate the string to use it in a if function like this:

if(eval(inCatAlt(13))){
 do something
}

But it doesn´t work. Do I evaluate it wrong or what is the problem? If I copy paste the output into the if function it works fine…

Thanks in advanced guys. Is my first time asking something here. I´m curious :)

© Stack Overflow or respective owner

Related posts about Wordpress

Related posts about eval