A
A
Alexander Plyusnin2021-08-25 22:59:49
WordPress
Alexander Plyusnin, 2021-08-25 22:59:49

How to display the necessary terms of the post taxonomy?

Good day to all. I'm racking my brains on how to derive the necessary taxonomy terms.
Now

<?php                 
$cur_terms = get_the_terms( $postID->ID, 'wpsl_store_category');
// var_dump($cur_terms);
if( is_array( $cur_terms ) ){
    foreach( $cur_terms as $cur_term ){
        echo $cur_term->name;
    }
}
?>

Outputs this:
6126a037c9a3d777318092.jpeg

And I need to display only certain terms. Without "free" and "Public". In the admin panel there is a parent taxonomy and marked child ones.

6126a109b3814161750905.jpeg

Please tell me how can I derive these necessary terms.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Zolin, 2021-08-26
@bioWeb

get_the_terms()gets the terms related to the current entry. If you have "Free" and "Public" checked, the function will return them too. There are no arguments in this function that allow you to exclude terms from the selection at the time of the query to the database
. You can exclude the necessary terms when displaying them in the front, but the principle by which you want to do this is not entirely clear. The easiest way is to check for existence in an arbitrary array

$cur_terms = get_the_terms( $postID->ID, 'wpsl_store_category' );
$not_allowed = [ 'Бесплатно', 'Государственное' ];

if( is_array( $cur_terms ) ) {
  foreach( $cur_terms as $cur_term ) {
    if ( !in_array( $cur_term->name, $not_allowed ) ) {
      echo $cur_term->name;
    }
  }
}

V
Vladik Bubin, 2021-08-26
@ikoit

You can display the ones you need through an array or exclude those that do not need to be displayed. All details here: https://wp-kama.ru/function/get_taxonomies

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question