WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Is there a way to check if a single post is attached to a particular taxonomy (2 posts)

  1. dorich
    Member
    Posted 1 year ago #

    I want to create a condition whereby if a post is attached to a particular taxonomy then some additional code is executed on a modified single.php but I can't find a way to directly check for the taxonomy.
    I thought "is_tax" might do the job but it seems that this tag is only effective for archive pages, which I assume means a page that is generated from a template using the taxonomy name.

    Is there a direct way to read the taxonomy from a single post so that I can do something similar to:
    if (is_tax('chapter')) {additional code}

    Thanks

  2. dorich
    Member
    Posted 1 year ago #

    I worked out one solution for this as follows:

    $terms = get_the_terms( $post->ID , 'chapter');
     if($terms) { *additional code to be added to the template* }

    If any terms in the taxonomy 'chapter' are returned the additional code is added to the template otherwise the additional code is omitted.

    I don't know if this needs any additional error checking but the code as shown works by including code on the appropriate posts and omitting it where its not required.

    Any suggestions on improvements would be welcome.

Topic Closed

This topic has been closed to new replies.

About this Topic