How to display specific child page template in wordpress

admin2025-06-04  1

I am trying to display a child page template of a certain specific parent page.

<?php 
    if(has_children()){ 
        // This is a parent page 
        get_template_part('content', 'parent'); 
    }elseif(is_page() && $post->post_parent ){ 
        // This is a child page of certain parent page
        get_template_part('content', 'child');

        //*THIS PART BELOW HAS REFUSED TO WORK FOR THE CHILD PAGE OF PARENT PAGE ID 689
    }elseif(is_child_of(689)){ 
        // This is a child page of Parent page having id 689  
        get_template_part('content', 'park');
    }else{
        //This is default for anyother page
        get_template_part('content', 'default'); 
    } 
?> 

The code above, when trying to get the child page of the parent page(689), still renders or rather displays the get_template_part('content', 'child'); and not get_template_part('content', 'park');which i want.

I am trying to display a child page template of a certain specific parent page.

<?php 
    if(has_children()){ 
        // This is a parent page 
        get_template_part('content', 'parent'); 
    }elseif(is_page() && $post->post_parent ){ 
        // This is a child page of certain parent page
        get_template_part('content', 'child');

        //*THIS PART BELOW HAS REFUSED TO WORK FOR THE CHILD PAGE OF PARENT PAGE ID 689
    }elseif(is_child_of(689)){ 
        // This is a child page of Parent page having id 689  
        get_template_part('content', 'park');
    }else{
        //This is default for anyother page
        get_template_part('content', 'default'); 
    } 
?> 

The code above, when trying to get the child page of the parent page(689), still renders or rather displays the get_template_part('content', 'child'); and not get_template_part('content', 'park');which i want.

Share Improve this question edited Jan 17, 2019 at 20:21 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 17, 2019 at 19:45 Daniel OmaraDaniel Omara 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try this... Your code is hitting elseif(is_page() && $post->post_parent ) and stopping there.

<?php 
    if(has_children()){ 
        // This is a parent page 
        get_template_part('content', 'parent'); 
    }elseif(is_page() && $post->post_parent ){ 
        // This is a child page of certain parent page
        if(is_child_of(689)){
            // This is a child page of Parent page having id 689  
            get_template_part('content', 'park');
        }else{
            get_template_part('content', 'child');
        }
    }else{
        //This is default for anyother page
        get_template_part('content', 'default'); 
    } 
?> 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749000395a315507.html

最新回复(0)