I'm trying to exclude 2 (and potentially more) ID's from my loop, that displays all categories that have posts. The category ID's i'm currently trying to exclude are 1 and 19. I'm very new to PHP but have previously been able to fix my issues by searching on the forum but nothing so far has worked.
// Function to create shortcode of all categories
function my_categories_shortcode() {
// extracting categories
$args=array(
'orderby' => 'name',
'order' => 'ASC',
)
?><div class="categorylist">
<?php foreach (get_categories( $args ) as $cat ) : ?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<h3 class="categoryname"><?php echo $cat->cat_name; ?></h3></a>
<?php endforeach; ?>
</div>
<?php
}
// register shortcode
add_shortcode('showcategories', 'my_categories_shortcode');
What should i add, to exclude categories?
Thanks in advance!
EDIT
Thanks, i solved this by adding a continue function as mentioned by Tom Nowell. I have also added the hint that this is a string to the function:
// Function to create shortcode of all categories
function my_categories_shortcode() : string {
// extracting categories
$args=array(
'orderby' => 'name',
'order' => 'ASC',
)
?><div class="categorylist">
<?php foreach (get_categories( $args ) as $cat ) :
if ($cat->term_id == 19) {
continue;
}
?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<h3 class="categoryname"><?php echo $cat->cat_name; ?></h3></a>
<?php endforeach; ?>
</div>
<?php
}
// register shortcode
add_shortcode('showcategories', 'my_categories_shortcode');
: string
must return a string, but as I mentioned in my answer, your shortcode is built incorrectly and does not return its content as a string. Shortcodes can't echo or output directly, but that's what your shortcode function does, which is wrong and incorrect. Shortcodes that output directly via?>stuff<?php
orecho 'stuff';
break REST APIs, XMLRPC, RSS feeds, they don't work inside nested shortcodes, and they appear at the start of a post, not the location you put the shortcode. Do not directly echo or output HTML in a shortcode! Put it in a variable and return it