WP get_the_category() only returns 1 category or not returning (problem solved)
Category: Problems with code/cms and solution, WordPress
An example of old code that didn’t work well:
$category = get_the_category(); echo $category[0]->name;
An example of a correctly working code:
$cats = wp_get_post_categories( $post->ID, [ 'fields' => 'all' ] ); foreach( $cats as $cat ){ echo $cat->name; }
An example code for the case when you need to display subcategories other than the main one:
$cats = wp_get_post_categories( $post->ID, [ 'fields' => 'all' ] ); foreach( $cats as $cat ){ if ( ( $cat->term_id !== 1 ) && ( $cat->term_id !== 2 ) && ( $cat->term_id !== 3 ) ) echo $cat->name; // echo $cat->term_id; }
Covert testing code:
<?php if ( $ip != '00.00.00.00 ') { } else { } ?>