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 been trying to find a way to return page information in order to create a landing page. I have done this with posts before, to create a blog reel, and would like to achieve the same overall result with pages.

The Scenario: I use a drop menu with pages created in the WordPress. Nesting the pages builds the menu.

The Goal: I would like to get the subpages of the parent page. When a user navigates to the parent page I would like it to return links to the subpages with a part of the content of the child page. For example, I would like to display the <div> with class header.

A Starting Point:

$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');

foreach($mypages as $page)
{
    $content = $page->post_content;
    if(!$content) // Check for empty page
        continue;

    $content = apply_filters('the_content', $content);
?>
    <h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    <div class="entry"><?php echo $content ?></div>
<?php
}

So far function returns all of the_content for all of the children and grandchildren pages. I would like it to specifically return only 1 div with a specific class from each child page and disregard all of the grandchildren pages.

share|improve this question

migrated from stackoverflow.com May 9 '11 at 20:10

This question came from our site for professional and enthusiast programmers.

1  
@patrick: Where do you define a static page ID? You use $post->ID in the get_pages() call, doesn't this always refer to the current page ID? –  Jan Fabry May 10 '11 at 10:12
1  
@patrick: It's not really clear what your problem is: getting the child pages of the current page, or getting a part of these pages? If it is the latter, which part? Can you please edit your question to give examples of what you currently get and what you want to get? –  Jan Fabry May 10 '11 at 14:44
1  
@patrick: OK, can you specify what div it should be? What is the ID based on? Do you have an example of a full page, so we can see the div in context? –  Jan Fabry May 10 '11 at 14:57
1  
@patrick: OK, so you want to select the <div> with ID hdr from each post? And then probably rename the ID, to prevent ID collisions? –  Jan Fabry May 10 '11 at 15:15
1  
@patrick: I re-wrote your question, based on your comments. Is this what you had in mind? Otherwise feel free to roll it back. Is it always the first <div> you want to display? Would it be acceptable if the user had to "split up" the page using the More button? This is a bit easier to search for. –  Jan Fabry May 10 '11 at 15:31

2 Answers 2

up vote 1 down vote accepted

Two suggestions:

  1. To output your "Loop" only for Child Pages, and not for Grandchild etc. Pages, add a conditional.

e.g.

foreach ( $mypages as $page ) {
     if ( $page->post_parent == $post->ID ) {
          // Loop goes here
     }
}
  1. To output only an excerpt of each Child Page, enable excerpt support for Pages, and then output $page->post_excerpt.

In functions.php:

add_post_type_support('page', 'excerpt');

Then in your "Loop":

foreach ( $mypages as $page ) {

     if ( $page->post_parent == $post->ID ) {

              $content = $page->post_excerpt; // changed post_content to post_excerpt

              if( ! $content ) // Check for empty page
                   continue;

              $content = apply_filters( 'the_content', $content );
              ?>
              <h2><a href="<?php echo get_page_link( $page->ID ) ?>"><?php echo $page->post_title ?></a></h2>
              <div class="entry"><?php echo $content ?></div>
              <?php

     }
}
share|improve this answer
    
I have added support for pages and tried the new loop, but it just returns blank. –  patrick May 10 '11 at 16:43
    
My thinking was that you could put the custom excerpt content into the "Excerpt" form field on the Edit Page screen - i.e. the content within the specified <div> tags. –  Chip Bennett May 10 '11 at 17:00
    
I do not get a custom excerpt field. Does this mean I have not added support correctly? –  patrick May 10 '11 at 18:19
    
Did you check "Screen Options", to ensure that the "Excerpt" field is displayed? –  Chip Bennett May 10 '11 at 18:45
    
swish, thanks for helping me out with this Chip. –  patrick May 10 '11 at 18:58

And here is my solution to split it with the More button (which is also available for pages):

$child_pages = get_pages( array(
    'parent' => get_the_ID(),
    'hierarchical' => false,
    'sort_column' => 'post_date',
    'sort_order' => 'desc',
) );

foreach ( $child_pages as $child_page ) {
    $short_content = $child_page->post_content;
    if ( preg_match( '/<!--more(.*?)?-->/', $short_content, $matches, PREG_OFFSET_CAPTURE ) ) {
        $short_content = substr( $short_content, 0, $matches[0][1] );
    }
    echo apply_filters( 'the_content', $short_content );
}
share|improve this answer

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.