1

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');
5
  • 1
    hardcoding IDs means your website will break if you ever move it, or if those categories are deleted and recreated for whatever reason. At the very minimum use hardcoded slugs so you can recreate a term if that happens without changing code. – Tom J Nowell Jan 14 at 13:10
  • @TomJNowell Adding the ": string" as done in my edit stops the rest of the page from loading, could you explain what i've done wrong? Thanks! – Johan Schack Petersen Jan 14 at 14:15
  • Functions that have : 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 or echo '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 – Tom J Nowell Jan 14 at 15:28
  • See the example and re-read my answer, it includes examples demonstrating the difference, and comment on my answer instead to reply to it, especially the very last sentence – Tom J Nowell Jan 14 at 15:30
  • 1
    in addition to the comments and answers by @tom-j-nowell, have you checked the documentation of the function developer.wordpress.org/reference/functions/get_categories that there is an 'exclude' parameter; developer.wordpress.org/reference/classes/wp_term_query/… – Michael Jan 14 at 16:23
0

The solution is already in your code:

 <?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); ?>" />

More specifically:

 <?php foreach (get_categories( $args ) as $cat ) : ?>

...get_category_link($cat->term_id)...

That term_id is the ID of the category, so just test if it matches one of your undesired IDs and continue; to skip to the next one.


However, there is an unrelated but super critical mistake in your code. Shortcodes do not echo/output directly. They need to return their output in a string. This shortcode will break a lot of things, and will appear in the wrong place.

Remember:

function brokenshortcode() {
    ?><p>I am an awful shortcode that breaks lots of things</p><?php
}
add_shortcode( 'broken', 'brokenshortcode' );
function niceshortcode() : string {
    return '<p>I am a good well behaved shortcode</p>';
}
add_shortcode( 'nice', 'niceshortcode' );

You can enforce this by type hinting the function like this:

function my_categories_shortcode() : string {

Now the function will cause a PHP fatal if you make this mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.