• Resolved jamoboggins

    (@jamoboggins)


    I’m working on a custom template for a page which has a number of child pages.

    I’m trying to create an unordered list of sub-pages on the parent page. I’ve already installed a plugin that lets me add Excerpts to pages, and I want to use the excerpt to display a brief summary of each sub-page alongside the page titles. Once I have it working with the Excerpt, I plan to add thumbnail images by using custom fields for each sub-page.

    First I tried using wp_list_pages https://codex.www.ads-software.com/Template_Tags/wp_list_pages#List_Sub-Pages to call up the sub-pages of the parent page but this only displayed a list of the sub-pages. It seems I need to use the Loop to call up the Excerpt, and I couldn’t get wp_list_pages working with the loop.

    So instead I’ve tried using query_posts https://codex.www.ads-software.com/Template_Tags/query_posts#Retrieve_a_Particular_Page. I’ve been successful displaying one sub-page title and its excerpt (I’ll deal with the custom field later), but the query_posts seems to only let me call up one specific page rather than all of the sub-pages.

    I don’t really mind if I have to manually add the page titles to the query_posts tag in the template, though obviously it would be ideal if I could somehow call up all the sub-pages and still display the excerpt and custom field alongside the page titles (this only seems possible within the Loop).

    The code I’m currently using is below (the parent page is called “festivals” and the first sub-page is called “second-test-festival”). The main thing I want to know is whether it’s possble to add a second pagename to the query_posts tag.

    If there are other ways of acheiving what I want then I’d love to hear them too. Thanks!

    <?php
    query_posts('pagename=festivals/second-test-festival');
    ?>
    <ul>
    
                                <?php while (have_posts()) : the_post(); ?>
                                    <li>
                                        <span class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    
                                        <?php the_excerpt(); ?>
    
                                    </li>
                                <?php endwhile; ?>
                            </ul>
Viewing 7 replies - 1 through 7 (of 7 total)
  • It might be a bit more labor-intensive but what about using get_page_children instead and then using a simple foreach loop?

    Thread Starter jamoboggins

    (@jamoboggins)

    Thanks for replying, that sounds promising, but I’m afraid I’m a php beginner, and I’m not sure how to use get_page_children – how do I define the page_id as an integer and how do I define which page objects I want to call (I assumed I’d call things like the_content and the_excerpt in some following loops)?

    If someone could write out the code that would let me simply list all children of the current page that’d be great and I’m sure I’ll get how to use the loops after that.

    Thread Starter jamoboggins

    (@jamoboggins)

    This is the latest code I’m using on the parent page (which has ID 5). I’m trying to list the children of the page the code appears on, and display the child page titles, and an excerpt from each child

    <?php get_page_children( $page_id=5, $pages )?>
    
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    
    <li>
    <span class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    
    <?php the_excerpt(); ?>
    
    </li>
      <?php endwhile; ?>
    </ul>

    The page currently only displays the parent page title and excerpt (i.e. that’s what the loop is displaying), and seems to completely ignore get_page_children. I basically want the loop to be used for a number of pages in the same way that it can be when displaying a number of posts on the same page. Is this even possible?

    Could it be that get_page_children is already returning an array of objects and I need some more code to actually use them?

    I’m also unclear as to what ‘page objects’ it will return – is one object the_content and another the_excerpt etc? Or am I way off track?

    Sorry for my extreme lack of php knowledge, and thanks in advance for any help.

    Thread Starter jamoboggins

    (@jamoboggins)

    I’ve had some other advice, suggesting the following approach:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    
    <?php $parent = $post->ID; ?>
    
    <h2><?php the_title(); ?></h2>
    <div class="entry">
    <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    </div>
    </div>
    <?php endwhile; endif; ?>
    <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    
    <?php
    query_posts('post_parent ='.$parent);
    ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    <li>
    <span class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    <?php the_excerpt(); ?>
    </li>
    <?php endwhile; ?>
    </ul>
    </div>

    but the second loop calls up a post (the only post I have in my db) rather than the child pages. These are the functions that should call the child pages:

    <?php $parent = $post->ID; ?>

    and

    <?php
    query_posts('post_parent ='.$parent);
    ?>

    but at the moment these functions are calling a post instead of the child pages.

    Thread Starter jamoboggins

    (@jamoboggins)

    Right, thanks to some incredible help from someone off the forum, this is now working as I want it to.

    The key is to change query_posts so that it queries pages instead of posts. The key line of code is:

    <?php
    query_posts('post_type=page&post_parent='.$parent);
    ?>

    I am slightly worried about the ‘Important Note’ on the query_posts code page, as I think this is creating a second loop with query_posts but so far it seems to be OK.

    you want to use this to avoid the double looping.

    <div class="maincontent">
                   <?php the_ID(); ?>
                   <?php $parent = $post->ID; ?>
    <?php
    query_posts('post_type=page&post_parent='.$parent);
     while (have_posts()) : the_post();
    ?>
    
    <div class="imagelisting">
    <div class="Image">
        <?php $image = get_post_meta($post->ID, 'spots_img', true); ?>
     </div>
      </div>
    <?php endwhile; ?>
    </div>

    the query-posts is itself, a loop.

    There is no need to call the standard loop to set other php calls like
    <?php the_ID(); ?> or <?php $parent = $post->ID; ?>

    I’m trying to show a thumbnail in every post or excerpt but I’m a php beginner so I’ve decided to do this: I’ve uploaded a thumbnail folder.
    I want to show a thumbnail named “the post id” + jpg (or gif or so on).
    Does anybody know what should I do for getting it?

    Thanks a lot in advance !

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using query posts to list child pages’ is closed to new replies.