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 called glossary. I set up parent posts called "A", "B", etc. The glossary terms themselves are then set with the appropriate initial as their parent post. So, for example, "abdomen" has the "A" post set as its parent and "protein-skimmer" has the "P" post as its parent.

I now have a list of these initials,

<a href="/glossary/a/">A</a>
<a href="/glossary/b/">B</a>

When clicking on these links, I'm taken to a view which displays the individual "A" or "B" post.

What I'd like to see instead is "A" as a header, then a list of all of its children posts.

What's the most effective way to do this?

Thanks in advance,

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Try this, it's the most basic implementation. It could be more effecient to hook into pre_get_posts, and change the query from a 'top level page' to a query for it's children. But that has the potential to break a few things, and in particular would probably require you to alter the template used.

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

    <header class="entry-header">
        <h1 class="entry-title"><?php the_title(); ?></h1>
    </header><!-- .entry-header -->

    <?php   //If top level, find children
        if($post->post_parent == 0):
            $children = new WP_Query(array(
                'post_type'=>'page',
                'post_parent'=>$post->ID
            ));

            if($children->have_posts()): ?>
                <ul>
                <?php while ( $children->have_posts() ) : $children->the_post(); ?>
                    <li> <?php the_title();?> </li>
                <?php endwhile; ?>
                </ul>

            <?php else: ?>
                <?php echo 'No children';?>
            <?php endif; ?>

            <?php wp_reset_postdata(); ?>
        <?php else://Not top level, display normal post ?>
            <?php the_content(); ?>
        <?php endif;?>

    <?php endwhile; // end of the loop. ?>
<?php endif; ?>
share|improve this answer
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.