最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Adding multiple conditional tags in a function?

programmeradmin3浏览0评论

I have about 30 categories that need to be included within this function list:

add_filter( 'generate_blog_columns','tu_portfolio_columns' );
function tu_portfolio_columns( $columns ) {
    if ( is_category( '16' ) ) {
        return true;
    }

    return $columns;
}

I tried this:

if ( is_category( '16','12','45' ) ) {
        return true;
    }

What can I do to tweak this function to add more categories?

Thanks

I have about 30 categories that need to be included within this function list:

add_filter( 'generate_blog_columns','tu_portfolio_columns' );
function tu_portfolio_columns( $columns ) {
    if ( is_category( '16' ) ) {
        return true;
    }

    return $columns;
}

I tried this:

if ( is_category( '16','12','45' ) ) {
        return true;
    }

What can I do to tweak this function to add more categories?

Thanks

Share Improve this question asked Aug 8, 2020 at 9:35 HenryHenry 9831 gold badge8 silver badges31 bronze badges 1
  • Have you read the documentation? developer.wordpress/reference/functions/is_category – Jacob Peattie Commented Aug 8, 2020 at 10:09
Add a comment  | 

1 Answer 1

Reset to default -1

You want to put those category IDs into an array really, the IF can only handle one condition at a time, so you could change your if statement to be:

if( is_category('16') || is_category('12') ) {

In PHP, the || stands for OR, whilst && stands for AND, so your if statement is saying if category 16, OR category 12 in the above.

However, it makes more sense to do an array and loop through it, so you end up with something like this:

add_filter( 'generate_blog_columns','tu_portfolio_columns' );
function tu_portfolio_columns( $columns ) {
    $cats = array('16', '12', '45');

    foreach($cats as $cid) {
        if ( is_category( $cid ) ) {
            return true;
        }
    }

    return $columns;
}

It is untested, but I think it should do the trick for you... In line 3 we are creating the array of category IDs that you want, and then on line 5 we are putting that array into a foreach loop so each ID can be called once at a time. Then within that is your IF statement as it was which retrieves the current loop iterations category ID and checks it using WP's is_category function.

发布评论

评论列表(0)

  1. 暂无评论