0

So, on my wordpress site I have page called "Books" and sub-pages, each containing info about some book.

It looks something like this:

  • Books
    • Harry Potter, J.K.Rowling
    • Some Other Book, Who WroteIt
    • Some Other Book, Who WroteIt
    • Some Other Book, Who WroteIt

What I want to do is create custom page template for "Books" page which will list all sub-pages. I managed to find this:

<div id="submenu">
<ul>
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>
</ul>
</div>

Wich does the job but how could I create template which is going to use some more information about the book. i.e. book title, book cover picture, and some other information created with custom fields.

I'm not sure if I explained this right so I'll try to sketch it. This is what I am trying to get with page template for Books page: Sketch

Red text shows type of content that needs to be "pulled" from sub-pages.

2 Answers 2

1

Assuming that you're on a page template or a custom page template, the first thing you want to do is grab the current page ID, then use that ID to grab the children. Once you have the children, display the content for those children.

Try using get_children(); You can also try get_page_children but for my example, I'm using get_children:

<?php
    //assuming you're on a page template in the loop
    $this_page_id = get_the_ID(); //get this page ID 

    $args = array(
        'post_parent' => $this_page_id, //parent's ID
        'post_type'   => 'page', //just want pages
        'post_status' => 'publish' //only pages that are published
    );

    $children_array = get_children( $args ); //get children pages

    foreach( $children_array as $child_page ){
        //for each child page
        $child_id = $child_page->ID; //get the child ID
        echo get_the_title( $child_id ); //get the child title
        echo get_post_meta( $child_id, 'custom-field-name', true ); //get the child custom field
        echo get_the_content( $child_id ); //get the child content
    }

?>

Replace the "custom-field-name" with your custom field names when you use get_post_meta. Wp_list_pages is more suitable if you wanted to have a menu of your pages versus outputting information from the pages - similar to the way wp_list_categories works for categories. Hope that helps!

0

If you are using posts (rather than pages) and categories, you could do this by creating a custom archive template for all posts in the category books. As an example, this is the category page of "Image of the Week" on a photography blog. http://blog.keithberr.com/category/image-of-the-week/ In this case I copied category.php and saved it as category-21.php (21 being the ID for this particular category.) I then modified it to show the post date, the image and the title. You could customize it further to include an excerpt or custom fields.

I'm not sure what else you have on your site in the way of content. If you have other page content and a blog, you might be better served by creating a custom post type for books. You could then also use custom fields or taxonomies to add additional information. Then you could create a custom page to display all books by using a query.

For example I have a page on a demo site that displays an alphabetical list of all posts in the custom post type monsters. http://www.coolwebdev.com/givecamp/monsters/. The template is simply page-monsters.php. The code for the query is this:

<ul>
<?php $wp_query = new WP_Query(array('showposts' => 50, 
'post_type' => array('monsters'),'orderby' => 'title'));?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>  

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>
<?php endif; ?>

</ul> 

While I used an unordered list and just brought in titles you could change that to include an image, excerpt, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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