WordPress.org

Ready to get started?Download WordPress

Forums

get_the_date will not work with child pages (1 post)

  1. ScroogeHDL
    Member
    Posted 1 year ago #

    I woud like to create a list of child pages. all of the same parent. But I would like to insert the Year at the top of each section as appropriate. For example:

    2012

    Child page 1
    Child page 2
    Child page 3

    2011

    Child page 1
    Child page 3
    Child page 3

    I have tried the following code, but the get_the_date('Y') function always returns the year of the parent page and not the child pages. However, the child pages correctly display in a long uninterrupted list. How do I change the value of $this_year so that I can display the appropriate years as above?

    $query = new WP_Query( array(
    		'post_type' => 'page',
     		'post_parent' => '263',
     		'posts_per_page' => '-1' ) );
    $prev_year = NULL;
    
    // Loop
    while($query->have_posts()):
         $query->next_post();
         $id = $query->post->ID;
          $this_year = get_the_date('Y');
          // echo '<h3>' . $this_year . '</h3>';
          if ($prev_year != $this_year) {
              // Year boundary
              if (!is_null($prev_year)) {
                 // A list is already open, close it first
                 echo '</ul>';
              }
              echo '<h3>' . $this_year . '</h3>';
              echo '<ul>';
         }
         echo '<li>';
         echo get_the_title($id);
         echo '</li>';
         $prev_year = $this_year;
    endwhile;
       echo '</ul>';

    My site is currently on my localhost.
    Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic