Forums

Conditional for a Grandfather Page (2 posts)

  1. wireframe74
    Member
    Posted 1 year ago #

    Hi I have site where I need to include a template if a page is a descendent of a grandfather page.

    At the moment I have a conditional statement that searches for child pages of subcategory but I really need to have a conditional that looks for the main category instead. If I cannot get this to work the client will need me to update menu manually each time a new subcategory is created which is less than ideal.

    Here is what I am using at the moment:

    if ($post->post_parent == '69'|| $post->post_parent == '82'|| $post->post_parent == '15'|| $post->post_parent == '15') {
      include (TEMPLATEPATH . '/collection-seating-accordian.php'); 
    
    }

    The id of the main category/grandfather page is 71.

    Thanks!

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    it is totally not clear where you are applying your conditional statement, because your are mixing pages and categories:
    the following is for the category archive:

    this will build an array with all the ancestor category ids of the current category archive; and check for the grandparent ancestor of the category:

    $cat_id = get_query_var('cat'); // get the category id for the test
    	$cat_ancestors = array();
             $grandparent_cat = '';
    	$cat_ancestors[] = $cat_id;
    	do {
    	$cat_id = get_category($cat_id);
    	$cat_id = $cat_id->parent;
    	$cat_ancestors[] = $cat_id; }
    	while ($cat_id);
    	if($cat_ancestors[2]) $grandparent_cat = $cat_ancestors[2];

Topic Closed

This topic has been closed to new replies.

About this Topic