$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to display only 3 main categories, separated by commas, since they are marked in the post?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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

           }
        }
    }
}
Post a comment

comment list (0)

  1. No comments so far