admin管理员组

文章数量:1333185

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; ?>

本文标签: On the Blogpost I have categories showingis there a way to exclude a catgegory like 39Media39