Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!