0

In my theme's article page (single.php) I have the following code filtering a div to be shown only on certain category:

<?php $categoria_post = the_category_id($categoria_post);
if ($categoria_post == 760) : ?>
<div>
...
</div> 
<?php endif; ?>

Now I need to filter other div considering only the taxonomy slug (series_especiais), ignoring the id:

<?php $especiais = the_terms('slug','taxonomy-name');
if ($especiais == 'series_especiais') : ?>

But this code above doesn't work.

EDIT:

The way this taxonomy was created:

add_action( 'init', 'create_post_tax' );

    function create_post_tax() {
        register_taxonomy(
            'series_especiais',
            'post',
            array(
                'label' => __( 'Séries Especiais' ),
                'rewrite' => array( 'slug' => 'series_especiais','with_front' => true ),
                'hierarchical' => true,
            )
        );
    }

1 Answer 1

1

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!

12
  • Sorry, but I can't find the id of the taxonomy I want. That's where I'm stucked.
    – aarede
    Jul 7, 2016 at 20:12
  • @aarede Do you mean the name of the taxonomy? When you're viewing the terms in the admin, check the URL of the page - the name (or rather, slug) of the taxonomy should be in there.
    – Tim Malone
    Jul 7, 2016 at 20:50
  • this taxonomy was created (by a person who's not in the project anymore) to group 2 categories. I searched some ID even in database, but there's no ID number related to this custom taxonomy (named 'series_especiais'). Now I need to add a filter that includes a div only on articles under this 'series_especiais' custom taxonomy. The first code example in my question works fine filtering a div to be shown in certain category ID articles, but now I need to do the same on that pseudo-category apparently without ID.
    – aarede
    Jul 7, 2016 at 21:37
  • @aarede It sounds like there might be some confusion between a term and a taxonomy. The code I gave you, building from what you had originally, assumes series_especiais is the slug of the term. Is it in fact the slug of the taxonomy? (Note that, for clarity's sake, category is the slug of the default category taxonomy, where, eg. posts-about-me might be the slug of a term under the category taxonomy).
    – Tim Malone
    Jul 7, 2016 at 21:40
  • Yep, 'series_especiais' is the slug of the taxonomy.
    – aarede
    Jul 7, 2016 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.