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 5 main posts within a custom post type. Some of these posts will have children. I have content that should only be displayed on the parent post and all children of that post. What's the best way to achieve this?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You could setup a condition based on post ID:

<?php if (is_page(12) || $post->post_parent=="12") { ?>

    <ul>
        Your Menu
    </ul>

<?php } else { ?>

    <ul>
        Another Menu (or put nothing here)
    </ul>

<?php } ?>

If you don't know how to locate the post ID:

"Roll over any page, post or category with your cursor (while logged in and in the "manage" area of course), and the page, post or cat_id number appears in the progress bar in the bottom of the browser." - http://wordpress.org/support/topic/where-is-the-post-id-number

share|improve this answer
    
Thanks! It's showing the content on the child pages but not the parent. How do I show it on the parent as well? Thanks for your help. –  semory Mar 26 '13 at 21:41
    
the if(is_page(Your-post-ID) should be doing that. Did you include that portion? –  RevCocnept Mar 26 '13 at 22:02
    
I ended up changing is_page to is_single and that did the trick. Thanks again for the help! –  semory Mar 26 '13 at 22:06
    
Oh, yes! My bad. So glad you got it working though! –  RevCocnept Mar 26 '13 at 22:54

Your question is a little unclear...I think what you're trying to ask is how to display the parent content for that post...and then also the content for each child?

Try this:

FOR THE PARENT (let's say your content is the title)

<?php if (have_posts()) : ?> 

        <h1><?php the_title(); ?></h1> 

    <?php while (have_posts()) : the_post(); ?>

FOR THE CHILDREN

<?php

    global $post;
    $child_pages_query_args = array(
        'post_type'   => 'your-custom-type',
        'post_parent' => $post->ID,
        'orderby'     => 'menu_order'
    );

    $child_pages = new WP_Query( $child_pages_query_args );

    if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
?>      

        <div>SOME CHILD CONTENT</div>

END CHILDREN

<?php 

        endwhile; endif;

        wp_reset_postdata(); 

?>          

END PARENT

<?php endwhile; endif; ?>

The whole thing together will look like this:

    <?php if (have_posts()) : ?> 

    <h1><?php the_title(); ?></h1>

    <?php while (have_posts()) : the_post(); ?>

    <?php

    global $post;
    $child_pages_query_args = array(
        'post_type'   => 'your-custom-type',
        'post_parent' => $post->ID,
        'orderby'     => 'menu_order'
    );

    $child_pages = new WP_Query( $child_pages_query_args );

    if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
        ?>                              

            <div>SOME CHILD CONTENT</div>           

    <?php 

    endwhile; endif;

    wp_reset_postdata(); 

    ?>


    <?php endwhile; endif; ?>
share|improve this answer
    
Sorry it was unclear. If I have a custom post type named Markets and under Markets are 5 posts (auto, aerospace, marine, rail, energy). if, say, Rail, needs a menu in the sidebar on the parent and all children then I am looking for a conditional statement that will show the menu only on those specific pages. I am using one template file named "single-market.php". Is that clearer? –  semory Mar 26 '13 at 20:55
    
So when you say your posts "auto", "aerospace" etc. Is "auto" just the title of that post? –  RevCocnept Mar 26 '13 at 21:03
    
Yes, that's correct. –  semory Mar 26 '13 at 21:27

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.