Wow, this on really made my head hurt for a bit.
So I’ve been building a WordPress site hat is using a custom post type of “help.” As you can probably guess, this is for a help section. The data on these help pages is fairly extensive, so I set my post type up to be hierarchical. No problem so far.
Next, I setup a page template for these custom posts (single-help.php). This is where it got a bit tricky; I had three levels of data, all which needed a functional editor so users could input html, and each level needed its own layout, pulling in only its sub posts. So essentially, my page template ended up looking like so (albeit a bit more convoluted).
<?php // setup your arguments: $args = array( // We want to call all posts the custom post type 'help' 'post_type' => 'help', 'posts_per_page' => -1, 'post_status' => 'publish', 'order' => 'ASC', // Next, we want to see if the current post is a parent of any help posts 'post_parent' => $post->ID ); // We then pass these to a wordpress query, and setup our loop $loop = new WP_Query( $args ); ?> <?php if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); ?> <!-- This post has children, so we'll list them out --> <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a> <?php endwhile; ?> <?php else : ?> <!-- This post has no children --> <?php endif; ?>
So basically, this code allowed my single-help file to handle all three levels of the help page hierarchy, loading in each when necessary.
Lemme know your thoughts. I’d love to clean this up a bit more.
Leave a Reply