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; ?>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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; ?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0