• I have a Page ID and I just want to check whether that Page is the immediately child of the Top Parent.

    I just need a boolean true or false.

    Please help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter fas.khan

    (@faskhan)

    Any Help, please.

    I will rephrase it.

    With the help of the ID, I just want to see whether the page with the ID is the Top Parent.
    (At level 0)

    If it is at the second level (Level 1), I want to return false.

    For e.g.

    Home Link 1
    Home Link 2
    Home Link 3
    +++ Sub Link 31
    +++ Sub Link 32
    +++ Sub Link 33
    Home Link 4
    +++ Sub Link 41
    +++ Sub Link 42
    —+++ Sub Link 421
    —+++ Sub Link 422

    Now, if I get the Page ID of Home Link 3, I should have true, as it is the first parent. (And also true for Home Link 4 etc.)

    But, if I get the ID for Sub Link 42 (which is also a parent), my code should return false, as it is not at the Top Level.

    Many Thanks.

    You might could use get_category_parents()?

    It doesn’t return a boolean, but I think it would get the job done.

    Thread Starter fas.khan

    (@faskhan)

    Thank you for both the answers.

    But the first one tells whether the parent is category.

    And the second one takes the $post handler to tell whether the current page is parent or not.

    But I have only the ID and I cant proceed with the $post variable.

    Within my code, which is as follows, I am able to found out by ID that whether the current page is Parent or not.

    // Getting the Page ID in $sel_page_id.
    
    foreach($include_array as $sel_page_id)
    {	
    
    //Is it a parent
    if(wp_list_pages("title_li=&child_of=$sel_page_id&echo=0" )):
    
    echo $sel_page_id." is a parent";
    
    //else it is not!
    endif;

    It works perfectly but the only problem, it gives me all the parents. For example, It will give me with respect to the upper example, the following Pages.

    Home Link 1
    Home Link 2
    Home Link 3
    Home Link 4
    Sub Link 42 <—– I just dont want that, as it is not the Top Parent.

    Please help .. ??

    Thread Starter fas.khan

    (@faskhan)

    I did it with this code.

    global $wpdb;
    					$prevalholder = array();
    					$arrctr = 0;
    					$matchFlag = false;
    					foreach($include_array as $sel_page_id)
    					{
    						$matchFlag = false;
    
    						if(wp_list_pages("title_li=&child_of=$sel_page_id&echo=0" )):
    
    							foreach($prevalholder as $previousValues)
    							{
    								// Check if this ID has any Parent. If it returns Zero, than there is no parent.
    								$idchecks = $wpdb->get_var( "select post_parent from $wpdb->posts where ID = $sel_page_id");
    								if ($idchecks == 0)
    								{
    									//It is a Top Parent
    									$matchFlag = false;
    									//echo "Not caught";
    								}
    								else
    								{
    									// It is not a Top Parent
    									$matchFlag = true;
    									//echo $sel_page_id . "CAUGHT    " ;
    								}
    
    							}
    							if (!$matchFlag)
    							{
    								//The current ID is the Top Parent. Add it in the Array.
    								$prevalholder[$arrctr] = $sel_page_id;
    								$matchFlag = false;
    								$arrctr ++;
    							}
    
    						endif;
    
    					}
    					//$prevalholder Array now holds all the Top Parent IDs.
    					foreach($prevalholder as $pVal)
    					{
    						echo "<br />Top Parent ID :  ". $pVal;
    					}
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Checking whether Page is the immediate Child’ is closed to new replies.