How to display only 3 main categories, separated by commas, since they are marked in the post?

admin2025-06-02  1

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>

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>
Share Improve this question edited Feb 27, 2019 at 18:42 Omniatom asked Feb 27, 2019 at 18:18 OmniatomOmniatom 596 bronze badges 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? – Fabrizio Mele 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
Add a comment  | 

4 Answers 4

Reset to default 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>

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; ?>

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

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

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>' ; 

           }
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748837640a314138.html

最新回复(0)