On the Blogpost I have categories showing, is there a way to exclude a catgegory like 'Media'?

This is the code I have in a include file on single template file.Is it possible to exclude 'Media' showing a

This is the code I have in a include file on single template file.

Is it possible to exclude 'Media' showing as a category.

So for example if I have the following categories on a post - 'Test Test1 Media' then it should show on the blog post 'Test Test1' ONLY.

        <?php
            $categories = get_the_category();
            foreach($categories as $cat):
        ?>

            <a href="<?php echo get_category_link($cat->term_id); ?>">
                <?php echo $cat->name; ?>
            </a>

        <?php endforeach; ?>

This is the code I have in a include file on single template file.

Is it possible to exclude 'Media' showing as a category.

So for example if I have the following categories on a post - 'Test Test1 Media' then it should show on the blog post 'Test Test1' ONLY.

        <?php
            $categories = get_the_category();
            foreach($categories as $cat):
        ?>

            <a href="<?php echo get_category_link($cat->term_id); ?>">
                <?php echo $cat->name; ?>
            </a>

        <?php endforeach; ?>
Share Improve this question asked Jul 9, 2020 at 3:10 Rejaur RahmanRejaur Rahman 51 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Add this simple check to your foreach loop:

<?php
$categories = get_the_category();
foreach($categories as $cat):
    if ( $cat->name == 'Media' ) continue;
?>

    <a href="<?php echo get_category_link($cat->term_id); ?>">
        <?php echo $cat->name; ?>
    </a>

<?php endforeach; ?>

You can check the category name against the list of categories:

<?php
$categories = get_the_category();
foreach($categories as $cat):
    if ( in_array( $cat->name, array( 'Media', 'Other Category 1', 'Other Category 2' ) ) ) continue;
?>

    <a href="<?php echo get_category_link($cat->term_id); ?>">
        <?php echo $cat->name; ?>
    </a>

<?php endforeach; ?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281201a4414516.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信