Filtering elements by taxonomy slug

Have a look at the documentation for the_terms(). It requires at least two parameters – the post ID, and the taxonomy name. You’re not giving it either of these (I am of course assuming your taxonomy isn’t called taxonomy-name).

However, you’re also using the wrong function for the situation. the_terms() is used to display (i.e. echo out) a list of terms, not to compare them in an if statement. The function you’re after is get_the_terms(). One thing to note is that a lot of WordPress functions follow a similar naming scheme: generally, a function starting with get_ will return the value (great for comparisons, or for saving for later), whereas a function starting with the_ will echo the value out.

So, let’s look at get_the_terms(). Looking at the documentation, the required parameters are similar: the post ID (or post object), and the taxonomy name. So, we’d be looking at something like this:

$especiais = get_the_terms( $post, 'taxonomy-name' );
// replace taxonomy-name with the name of your taxonomy!

This will return an array of term objects, rather than a single term (you can verify this by running print_r( $especiais ); – using print_r() is a great way to confirm what data you’re getting back from a function, and is generally a good habit to get into when you’re debugging code).

Then finally, we need to check through that array for the slug you’re after. But not before we confirm that we actually did get an array back – you can never be too sure (especially if there were no terms assigned at all, or we made a mistake in the name of our taxonomy):

if( is_array( $especiais ) && count( $especiais ) ) { // is it an array, and with items?
  foreach( $especiais as $term ) { // loop through each term...
     if( $term->slug === 'series_especiais' ) { // ...till we find the one we want!
       ?>
       <div> put your div and other content here </div>
       <?php
       break; // avoid searching the rest of the terms; we've already found what we want
     }
  }
}

So the lesson to learn is: always look up the documentation for the functions you use. The official Code Reference is an excellent place to start, otherwise often just Googling the function name will help too.


EDIT: After our discussion in the comments, it turns out there was a bit of confusion over terms and taxonomies. Given in your case the taxonomy name is ‘series_especiais’ and you want to just determine whether the post has any term in this taxonomy, you can skip the foreach loop entirely. Here’s a rewrite:

$especiais = get_the_terms( $post, 'series_especiais' );

if( is_array( $especiais ) && count( $especiais ) ) { // is it an array, and with items?
  ?>
  <div> put your div and other content here </div>
  <?php
}

Much simpler 🙂 I’ll leave the rest of the above code here too because it might help someone else!