1

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

  • 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.

5
  • This would probably require a complex custom query. Why not use a custom post type instead of pages? This may make it slightly easier.
    – Chris
    Commented Aug 8, 2011 at 15:52
  • @Chris Cheers, like I said though, posts aren't an option. I've been looking at this - codex.wordpress.org/Function_Reference/wp_list_pages, surely it can be done using that?
    – Rob
    Commented Aug 8, 2011 at 15:56
  • Actually it can't be done using wp_list_pages. Pages by default are not mean to have an archive, hence the capability is not there. To accomplish this, you will need a custom query.
    – Chris
    Commented Aug 8, 2011 at 16:03
  • You'd only need run some custom code after the page fetching to re-sort that data into arrays by month/year(using a page template of some sort). I'll provide an example when i'm back at my regular PC. I'd not suggest wp_list_pages that's just not the right tool for this, get_posts() or get_pages() would be more appropriate because you're going to be resorting the return data anyway(might aswell query the pages more directly).
    – t31os
    Commented Aug 8, 2011 at 20:01
  • @Mark Ok cheers, that would be great.
    – Rob
    Commented Aug 9, 2011 at 7:45

1 Answer 1

2

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)..

Your Answer

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

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