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
1 Answer
Reset to default 1Add 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条)