How to return a Category ID in Wordpress?

Asked

Viewed 476 times

0

I need to return the ID within this function:

$field = odin_get_term_meta( CATEGORY ID HERE, 'cattitulo' );

I tried it with get_term_meta() and get_terms() and nothing. I imagine it’s pretty simple, I don’t know where I’m going wrong.

1 answer

1


You can use get_term_by() with the data you have of the category.

Ex.:
// Retorna o objeto da categoria Animais Fofinhos
$categoria = get_term_by( 'slug', 'animais-fofinhos', 'cattitulo' );

The first parameter accepted slug, name, id or term_taxonomy_id.

e depois:
$field = odin_get_term_meta( $categoria->term_id, 'cattitulo' );
  • Thanks Ricardo! But how to call the 'cute animals' automatically there in get_term_by ? (in my case it would be within the category page)

  • @Guilhermesierra if you are in a category file, you can pick up the object in the Press $wp_query->queried_object or directly the id in $wp_query->queried_object_id.

  • Perfect! 100% Works. Thank you

  • Cool @Guilhermesierra. If solved, mark the answer as chosen so people know.

Browser other questions tagged

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