List pages within a certain parent and show published month

How can I list all pages within a certain parent (ID=917) and show the month and year they were published in this format:

Aug 2011

Read More
  • This is a page (link to page)

  • This is another page (link)

Jul 2011

Jun 2011

  • This is another page (link)

etc etc

UPDATE:
Also, just in case, how can I just show the months that have a page within it? So from the example above, it wouldn’t show Jul.

Please don’t tell me to use posts instead of pages as that isn’t an option on the setup I have.

Related posts

Leave a Reply

1 comment

  1. Ok firstly, create a php file inside your theme’s folder and give it a name that won’t clash with any of the native theme template files, archive.php, index.php etc… (give it a unique name, something like page-list-by-month.php).

    Add the following code to that file.

    <?php
    /**
     * Template Name: Pages by Month
     */
    get_header(); 
    ?>
    
    <!-- Your opening outer HTML -->
    
    <?php 
    $args = array( 
        'post_type' => 'page', 
        'orderby' => 'date', 
        'order' => 'asc', 
        'nopaging' => 1, 
        'post_parent' => 146 // <--- Adjust this to the appropriate parent
    );
    $data = array();
    
    query_posts( $args ); 
    
    if( have_posts() ) :
        while( have_posts() ) : the_post();
    
            $date = explode( '%', get_the_date( 'Y%m' ) );
            $year = $date[0];
            $month = $date[1];
            $data[$year][$month][] = get_the_ID();
    
        endwhile;
    endif;
    
    // testing
    // print '<pre>';print_r( $data );print '</pre>';
    
    foreach( $data as $year => $months ) {
        foreach( $months as $month => $page_ids ) {
            echo date( 'F', mktime( 0, 0, 0, $month ) ) . ' 2011<br />';
            foreach( $page_ids as $page_id )
                echo '<a href="' . apply_filters( 'the_permalink', get_permalink( $page_id ) ) . '">' . apply_filters( 'the_title', get_the_title( $page_id ) ) . '</a><br />';
        }
    }
    ?>
    
    <!-- Your closing outer HTML -->
    
    <?php //get_sidebar(); ?>
    <?php get_footer(); ?>
    

    Save the file.

    Now create a page and select the Pages by Month template in the Page Attributes metabox, what you enter for the content and title doesn’t matter, though the title you give this page will determine the URL, so consider where you want this special page to appear and use a title appropriate for that.

    Example title -> My Page Archive = http://example.com/my-page-archive

    Once the page is created and the template has been attached, that’s it..

    NOTE: Be sure to adjust the post_parent value in the args array and additionally be sure to add appropriate HTML where indicated(the markup will differ depending on theme so i left it out the sample).

    Hope that helps.. 🙂 (oh and Mark would be me – when i’m not at my regular PC)..