Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

367
Vistas
Check if category of post is x or is child of x

My wordpress site has categories with a simple hierarchy –

  • Blog
    • Role Shift
    • Urod the Last Show
    • News

I'm using the if statement below to catch if the post is in category blog or any of the child categories - and it works - but it feels stupid not to be able to just check the parent of the current category (plus I might want to add categories later).

if ( is_category('blog') || in_category(array('role-shift', 'urod-the-last-show', 'news')) )

I've searched and tried every suggestion - including cat_is_ancestor_of - but nothing is working.

Please help!

Robert

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

$categories = get_the_category(); //returns categories
$thiscat = $categories[0];

$parent_id = $thiscat->parent; //the parent id
$parent = get_category($parent_id) //this returns the parent category as an object

//use id or slug of category you are searching for
if( $parent_id == 1 || $thiscat->slug == 'blog' ){
//this is a child of blog or the blog page
}

This should do the trick. This will determine if the current category is a child of the blog page. The first part, get_category, returns the current category.

Then you can get the parent id from the current category and use 'get_the_category_by_ID' to get the parent category object.

Then you can check if you are under the parent category you want.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda