WordPress.org

Ready to get started?Download WordPress

Forums

Conditional for term_description() (6 posts)

  1. Pete
    Member
    Posted 1 year ago #

    I have this template file (taxonomy-xyz.php), as it shows each tag/term I'd also like to show the term decription BUT only if the tag/term has a description.

    Thanks

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    <?php
    $description  = term_description();
    if ( ! empty( $description ) ){
      // there is a term description
      echo $description;
    }
    ?>

    http://codex.wordpress.org/Function_Reference/term_description

  3. Pete
    Member
    Posted 1 year ago #

    Thanks for that, works great... can I ask how I'd prefix this with some html (that also only shows if the term description is present.

    Cheers,
    Pete

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Because term_description() wraps the description in a paragraph I changed the code a little so you can use your own html. in this example I wrapped the description in a h1:

    <?php
    $description  =  get_queried_object()->description;
    if ( ! empty( $description ) ) : ?>
    <!-- html here -->
    <h1>
    <?php echo $description; ?>
    </h1>
    <?php endif; ?>
  5. Pete
    Member
    Posted 1 year ago #

    That's great.. thanks so much. A useful little piece of code that!

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. Glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic