Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type where I've created 3 pages in a parent-child relationship. The pages are created like this : PARENT-CHILD-GRANDCHILD

I want the child and the grandchild pages to get the same content as the parent page and everything I add from the dashboard in the content area to be added after the parent post content.

For example if my parent post contains few lines of text and in my child post I will add another few lines of text this should be added after the parent content.

Here is the function for creating the CPT hierarchy :

function wpse121567_get_cpt_hierarchy() {
// Make sure it's the right CPT
if ( 'debate-parent' != get_post_type() ) {
    return false;
}
// Globalize the post object
global $post;
// Parent CPT
if ( 0 == $post->post_parent ) {
    return 'debate-parent';
}
// Not a parent CPT, so fetch current post parent
$parent = get_post( $post->post_parent );
// Child CPT
if ( 0 == $parent->post_parent ) {
    return 'debate-child';
} else {
    return 'debate-grandchild';
}
}

And this is my single-{myCPT.php} :

 <div id="content" class="span9" role="main">


<?php if ( 'debate-parent' == wpse121567_get_cpt_hierarchy() ) {
    // Normal loop here
    if ( have_posts() ) : while ( have_posts() ) : the_post();

    get_template_part( 'inc/post-format/single', get_post_format() ); 
    endwhile; endif;
} 
// Child CPT
else if ( 'debate-child' == wpse121567_get_cpt_hierarchy() ) {
    // Globalize post object
    the_post();
        $parent = get_post( $post->post_parent );
        echo '<h1>' . $parent->post_title . '</h1>';
        $content = apply_filters( 'the_content', $parent->post_content ) . get_the_content();
        echo '<div>' . $content . '</div>';

    // Fetch parent CPT comments
    $parent_cpt_comments = get_comments( array(
        'post_id' => $post->post_parent,
        'status' => 'approve'
    ) );
    // Loop through parent CPT comments
    foreach ( $parent_cpt_comments as $comment ) {
        // Output comment list markup here
    }
}
// Grandchild CPT
else if ( 'debate-grandchild' == wpse121567_get_cpt_hierarchy() ) {
    // Comment Stats code goes here

 }
?>

    <?php 


        $enable_rtl         = of_get_option('enable_rtl', false);



        if(!of_get_option('disable_pagination')){

            if($enable_rtl){

                $next_post = get_adjacent_post( false, '', true ); 

                $prev_post = get_adjacent_post( false, '', false ); 

            }else{

                $next_post = get_adjacent_post( false, '', false ); 

                $prev_post = get_adjacent_post( false, '', true ); 

            }

            ?>

            <?php

        }



    // show related posts by tag

    if(!of_get_option('disable_related_posts')){ 

        get_template_part( 'inc/related-posts' );

    }





    // If comments are open or we have at least one comment, load up the default comment template provided by Wordpress

    if ( comments_open()  )

        comments_template( '', true );



 else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>



<article class="post error">

    <h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>

</article>



<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>

</div><!-- #content .site-content -->

My question is how can I automatically get the parent post content on every child and if I add more content from the dashboard to be added after the content from the parent element?

Thank you!

share|improve this question
    
Are you ever going to have a hierarchy more than two levels deep? –  Andrew Bartel Nov 8 '13 at 18:44
    
No, two levels is maximum. –  Alecs Nov 8 '13 at 18:45
add comment

2 Answers

up vote 1 down vote accepted

My question is how can I automatically get the parent post content on every child

Your code should work just like it is: if you have a parent and you add any number of children to this parent (wpse121567_get_cpt_hierarchy return 'debate-child' for them), your code will show the content of parent post.

and if I add more content from the dashboard to be added after the content from the parent element?

for this, where you have:

global $post;
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>'; 

replace with

the_post();
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
$content = apply_filters( 'the_content', $parent->post_content ) . get_the_content();
echo '<div>' . $content . '</div>';
share|improve this answer
    
I have a parent and two children but is not retrieving the parent post content, it's displaying just the content which I add inside the child not the parent post content. Any idea what's wrong? –  Alecs Nov 8 '13 at 21:04
    
@Alecs and this happen with your code or with the code I posted? –  G. M. Nov 8 '13 at 21:35
    
This is happening with the code I posted... –  Alecs Nov 8 '13 at 21:38
    
@Alecs I noticed there was a problem on your code. not calling the_post();. I've edited my answer. –  G. M. Nov 8 '13 at 21:51
    
Something very strange is happening now, I get " PAGE NOT FOUND" but I'm getting the same error with my code too which was working fine. I've edited my question with the relevant part from single-{mycpt}.php –  Alecs Nov 8 '13 at 22:03
show 4 more comments

You need to retrieve the grand parent and parent content and then display that before you display the standard content of the post itself.

So, in the loop itself, check to see if this post has a grand parent and/or parent, and then display that, otherwise just skip it.

get_header();

while( have_posts() ) {
the_post();
$post = get_post(get_the_ID());

// if this post has a parent, retrieve it
$parent = ($post->post_parent != 0) ? get_post($post->post_parent) : null;

// if this post has a grand parent, retrieve it by referencing the parent's parent
$grandparent = (is_object($parent) && $parent->post_parent !=0 ) ? get_post($parent->post_parent) : null;

// if we have a grandparent, retrieve the comments for later use and display the grandparent's title and content
if( is_object( $grandparent ) && $grandparent->post_content ) {

    $grandparent_comments = get_comments( array(
        'post_id' => $grandparent->ID,
        'status' => 'approve'
    ) );

    echo '<h1>'.$grandparent->post_title.'</h1>';
    echo '<div>'.apply_filters('the_content',$grandparent->post_content).'</div>';
}

// if we have a parent, retrieve the comments for later use and display the parent's title and content
if( is_object( $parent ) && $parent->post_content ) {

    $parent_comments = get_comments( array(
        'post_id' => $parent->ID,
        'status' => 'approve'
    ) );

    echo '<h1>'.$parent->post_title.'</h1>';
    echo '<div>'.apply_filters('the_content',$parent->post_content).'</div>';
}

// echo the content of the actual post we're on
the_content();

// if the grandparent post has comments, we check to see if it's an object because if there are no comments get_comments() returns an array rather than an object
if( isset( $grandparent_comments ) && is_object( $grandparent_comments ) ) {
    foreach($grandparent_comments as $grandparent_comment) {
        // echo comments
    }
}

// if the parent post has comments
if( isset( $parent_comments ) && is_object( $parent_comments ) ) {
    foreach($parent_comments as $parent_comment) {
        // echo comments
    }
}

$post_comments = get_comments( array(
    'post_id' => get_the_ID(),
    'status' => 'approve'
) );

if( is_object( $post_comments ) ) {
    foreach($post_comments as $post_comment) {
        // echo comments
    }
}
}
get_footer();
share|improve this answer
    
I'm a bit confused so how this will be integrated into the snippet I've posted ? –  Alecs Nov 8 '13 at 19:23
    
It replaces it all, I've updated the answer with comments in the code and added the comments loops –  Andrew Bartel Nov 8 '13 at 19:31
    
I'm not sure this is working, because the child of parent should display the comments submitted on the parent, and the grandchild should contain some custom functions to retrieve few comments from the child page,that's why I posted that code. Or this is working the same ? –  Alecs Nov 8 '13 at 19:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.