WordPress.org

Ready to get started?Download WordPress

Forums

Only show taxonomy if it is a certain value (10 posts)

  1. elhashbrown
    Member
    Posted 3 years ago #

    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?

  2. fonglh
    Member
    Posted 3 years ago #

    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?

  3. elhashbrown
    Member
    Posted 3 years ago #

    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.

  4. fonglh
    Member
    Posted 3 years ago #

    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';
  5. elhashbrown
    Member
    Posted 3 years ago #

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

  6. fonglh
    Member
    Posted 3 years ago #

    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.

  7. elhashbrown
    Member
    Posted 3 years ago #

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

  8. fonglh
    Member
    Posted 3 years ago #

    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.

  9. elhashbrown
    Member
    Posted 3 years ago #

    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!

  10. fonglh
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic