8. October 2011
von Blackbam
It is a common problem to detect parents or childs of pages and categories in WordPress correctly. The way WordPress stores relationships in hierarchies is not that trivial sometimes, so the results when querying are a bit confusing sometimes. It took some time to determine and optimize functions which do this job good and without faults, and I want to present these. As in this function I prefer working with page IDs, as this is the savest way to identify certain pages. With little changes it should also be possible to use these using e.g. slugs. Both of these functions can be used inside and outside of "the Loop" or loops.

Check if the current page (or a given page) is in the child tree of a given page

This function checks if the current page (or a given page, second parameter) is in the child tree of a given page.

/* Tests, if a page has got a certain parent anywhere in the ancestor/children tree */
function is_tree($pid, $pid2check = 0) {// $pid = The ID of the page we're looking for pages underneath
 
 global $post;
 // load details about this page
 
 // check if we should check two posts independently from the current query
 if ($pid2check < 1) {
  $pid2check = $post -> ID;
 }
 
 $anc = get_post_ancestors($pid2check);
 
 foreach ($anc as $ancestor) {
  if (is_page() && $ancestor == $pid) {
   return true;
  }
 }
 
 if (is_page() && (is_page($pid))) {
  return true;
  // we're at the page or at a sub page
 } else {
  return false;
  // we're elsewhere
 }
}

Check if the current post (or a given post) is in the ancestor/child tree of a given category

This function checks if the current post (or a given post identified by ID, second parameter) is in the ancestor/child tree of a given category.

/* Tests, if the given category is the current category, or in the ancestor/child tree of a given post or a child of it */
function is_category_or_sub($cat_id = 0, $pid = 0) {
 global $post;
 
 // check if a custom post id was given or get default (standard)
 if($pid<1) {
  $pid=$post->ID;
 }
 
 // check it for each category
    foreach (get_the_category($pid) as $cat) {
     if ($cat_id == $cat->cat_ID || cat_is_ancestor_of($cat_id, $cat))  {
      return true;
        }
    }
    return false;
}

Check if a given category is in the current category tree

Helpful if you have to determine the current category manually - checks if a given category is in the tree of another given category.

function in_category_tree($parent_cat,$child_or_equal_cat) {
 if($parent_cat ==$child_or_equal_cat || cat_is_ancestor_of($parent_cat, $child_or_equal_cat)) {
  return true;
 }
 return false;
}

Custom Taxonomy hierarchy check

Determine the top level taxonomy or check if a taxonomy is in the current taxonomies hirarchical tree.

function is_taxonomy_or_sub($child_or_equal_term_id,$parent_term_id,$taxonomy_name) {
       if($parent_term_id==$child_or_equal_term_id || $parent_term_id == get_top_level_term($child_or_equal_term_id,$taxonomy_name))  {
                return true;
        }
 }
 
function get_top_level_term($term_id,$taxonomy_name) {
 $term = get_term_by('id',$term_id,$taxonomy_name);
 if($term->parent>0) {
  return get_top_level_term($term->parent,$taxonomy_name);
 } else {
  return $term->term_id;
 }
}

This article is still under construction. Please report bugs, errors or improvement ideas, so we can optimize these scripts.

Share

Dieser Eintrag wurde am 8. October 2011 um 22:22 in der Kategorie WordPress, WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: , , ,

Already one comment belonging to "Determine if a WordPress page or category is in the current ancestor/child tree (inside/outside the loop)":

Kommentare abonnieren (RSS) or URL Trackback

Chrisp   says:

on 25. February 2012 at 20:39 on clock


Thank you, very insightful.

Leave a comment: