2

I'm trying to create a subcategory template of a defined category. For example, for each one, I would like to check if his subcategory Photos exists and to get posts of this.

Here is my category tree:

— Category Sessions
— — Subcategory Session 2015
— — — Subcategory Photos
— — — Subcategory ...
— — — Subcategory Videos
— — Subcategory Session 2014
— — — Subcategory Photos
— — — Subcategory ...
— — — Subcategory Videos
— — Subcategory Session 2013
— — — Subcategory Photos
— — — Subcategory ...
— — — Subcategory Videos

If subcategory Photos of this session category exists :

My idea is to have a query such as :

$wp_query = new WP_query(... Session 20XX/Photos ...&showposts=20);

Is there a way to do that please?

0

1 Answer 1

3
// Getting Parent ID
$parent = get_term_by( 'name', 'Session 2015', 'category');

// Checking if SubCategory Exist. 
$term = term_exists('Photos', 'category', $parent->term_id);

//if Exist Do the query
if ($term !== 0 && $term !== null) {
  $query = new WP_Query( 'cat='.$term['term_id'] );
}

References:

2
  • @M Haseeb Wooow! Thank you, it seems good. I'll try that.
    – Steffi
    Apr 20, 2015 at 9:30
  • BIM! Huge! Thank you so muck Haseeb. Exactly what I want.
    – Steffi
    Apr 20, 2015 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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