5

I want to display only 3 categories (Ex: horses, dogs, birds), names only and comma separated, in my Post, since one of them, two or all tree are marked in the post.

    <span><?php
if ( 'in_category('horses') ) {
    echo "horses";
} ?></span><span><?php
if ( in_category('dogs') ) {
    echo "dogs";
} ?></span><span><?php
if ( in_category('birds') ) {
    echo "birds";
} 
?></span>
2
  • You mean you want to restrict the number of categories shown under a post to only three? Which criteria you would like to use to decide which to show? Commented Feb 27, 2019 at 18:21
  • Id like to show them using a code in single.php, like in wp_list_categories, but in a way that I can define their IDs. Above is the code I´m using, but I cant make it with commas...
    – Omniatom
    Commented Feb 27, 2019 at 18:38

4 Answers 4

2

It should be enough using a single <span> for all categories and add some logic.:

<span><?php
$categories = ['horses','dogs','birds'];
$string = "";

foreach ($categories as $category){ //iterate over the categories to check
    if(has_category($category))
        $string .= $category.", "; //if in_category add to the output
}

$string = trim($string); //remove extra space from end
$string = rtrim($string, ','); //remove extra comma from end
echo $string; //result example: <span>horses, dogs</span>
?></span>
1
  • Simple and nice solution ... Commented Feb 27, 2019 at 19:43
1

I'm not sure, if I understand it correctly, because it doesn't sound like very common problem, but...

If you want to check only for the existence of given three categories and output them separated with commas, then this is the code you can use (based on yours, but I've fixed the empty spans problem):

<?php $cats_printed = 0; ?>
<?php if ( in_category('horses') ) : $cats_printed++; ?><span>horses</span><?php endif; ?>
<?php if ( in_category('dogs') ) : if ( $cats_printed++ ) echo ', '; ?><span>dogs</span><?php endif; ?>
<?php if ( in_category('birds') ) : if ( $cats_printed++ ) echo ', '; ?><span>birds</span><?php endif; ?>
0
1

Here's a simplified version of Fabrizo's code:

<span><?php
    $categories = [ 'horses', 'dogs', 'birds' ];
    echo implode( ', ', array_filter( $categories, 'has_category' ) );
?></span>
0

You can use get_the_terms( int|WP_Post $post, string $taxonomy ) . See Codex detail here.

// put IDs of your selected three categories e.g. 15, 18, 20  here in this array
$my_cats  = array(15, 18, 20);

$my_terms = get_the_terms( get_the_id(), 'category' );

if ( ! empty( $my_terms ) ) {
    if ( ! is_wp_error( $my_terms ) ) {

        foreach( $my_terms as $term ) {

           if(in_array($term->term_id, $my_cats){
              // Display them as you want
              echo '<span>' .  esc_html( $term->name ) . '</span>' ; 

           }
        }
    }
}
3
  • 1
    Thank you, but the code is gettig me error 500.
    – Omniatom
    Commented Feb 27, 2019 at 19:48
  • @Omniatom , Answer updated, there was a typo (missing . in last echo statement ) Commented Feb 27, 2019 at 20:04
  • Ok, great! Gonna keep this for later use. thank's for your time.
    – Omniatom
    Commented Feb 28, 2019 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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