最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to display specific child page template in wordpress

matteradmin8PV0评论

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'); 
    } 
?> 
Post a comment

comment list (0)

  1. No comments so far