Filter element from taxonomy in wordpress

Asked

Viewed 476 times

0

In the article page (single.php) of my theme I have the following code that filters a div making it appear only in pages of a certain category:

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

I need to filter a div similarly, but from the Slug of a taxonomy (special series), ignoring the id.

Something similar, but only filtered by Slug:

EDITED

How this custom 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,
        )
    );
}

2 answers

0

If you have the ID of the category in $categoria_post, you can use the method get_category()

$categoria = get_category($categoria_post)

and access it with

$categoria['slug']

Just remembering that the_category_id() is a deprecated method, so it is worth reevaluating your code. As you are in the single.php, it makes more sense to use the method wp_get_post_categories(). Using the_ID(), is as simple as

$cats = wp_get_post_categories(the_ID());

$cats is a array (even if the post has only one category). From there, is to search for the Slug and make your filter.

  • The problem is exactly this, taxonomy doesn’t have an id, it’s just a 2-sub-category binder. I would need to filter this taxonomy without id only by Slug.

  • All taxonomies (and categories) must have an ID. My answer was based on your code, from the ID you already claim to have.

  • Thanks, Caio. Did not know. Is there any way to filter only by Slug ignoring id?

  • You want to capture the slug of the post category, is this?

  • The Slug I have, I don’t have the id and I need to filter through this taxonomy. I only have his Slug.

  • So I don’t understand anything. I suggest you rephrase your question.

  • Perhaps the second Edit with the form that this taxonomy helps understanding.

Show 2 more comments

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.