WordPress.org

Support

Support » How-To and Troubleshooting » Only show taxonomy if it is a certain value

Only show taxonomy if it is a certain value

  • In my taxonomy pages within my template I list the (custom)posts of a particular taxonomy. Where the titles of these are listed I want to bring in the value of another taxonomy for certain posts(products) that are discontinued. However if it remains in production I want it to just have the title. The code I am using is:

    <?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'new-or-old', '', ', ', '' ) );
    echo $terms_as_text; ?>

    But this obviously shows the alternative taxonomies as well as discontinued. Can anyone help me with my novice PHP to structure some kind of ‘ if new-or-old = discontinued then print else don’t print’ code?

Viewing 9 replies - 1 through 9 (of 9 total)
  • So you have an alternative taxonomy called ‘new-or-old’ and one of the terms is ‘discontinued’?

    And if the post belongs to ‘discontinued’ you want it to print something?

    Yes that’s right so in the taxonomy archive page the titles have the term ‘discontinued’ next to them if they have that term but if they have the other term within that taxonomy (In production) then just the title is shown.

    I have used the above code to do this so far to produce text and css styles while hidding the ‘In production’ style. This works but I would much rather it is done properly with PHP.

    Try this, note that the string comparison is case sensitive.

    $terms = get_the_terms(get_the_ID(),'new-or-old');
    if(in_array('discontinued',$terms))
        echo 'Discontinued';

    Thanks. SOmething didn’t work though. It returned this:

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/convert/public_html/wp-content/themes/childtheme/taxonomy-brand.php on line 28

    Is it because the taxonomy archive is for ‘brand’ rather than ‘new-or-old’?

    No, it’s because the function actually returns an array of objects, rather than an array of strings.

    Does the code work correctly? If not we’ll have to try something else in the if condition.

    Ah I see. No the code isn’t working. The warning is coming up within the post title.

    oops it looks like that function returns a very complex array. It’ll be easier to check if the term list contains the word ‘discontinued’.

    $terms_as_text = strip_tags( get_the_term_list( get_the_ID(), 'new-or-old'));
    $pos = strpos($terms_as_text, 'discontinued');
    if($pos !== false)
        echo 'Discontinued';

    The PHP manual reference for the strpos function is here.

    That’s it! Works perfectly thank you, I really appreciate it. Helps me a lot to have some guidance as I try and learn all this PHP business!

    Glad to have been of help. Mark this thread as resolved please so others will know it’s been solved. I think the option is on the right side of the page.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Only show taxonomy if it is a certain value’ is closed to new replies.