Recent Posts Display Function
The avp_get_recent()
function queries the database for all published posts and pages, sorts them according to year, month and date, and presents the data in descending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | function avp_get_recent() { $all_posts = get_posts( array( ‘orderby’ => ‘modified’, ‘order’ => ‘DESC’, ‘post_type’ => array( ‘post’, ‘page’, ), ‘nopaging’ => ‘true’, ) ); // this variable will contain all the pages nd posts in a associative array // with three levels, for every year, month and posts. $ordered_posts = array(); foreach ( $all_posts as $single ) { $year = mysql2date( ‘Y’, $single->post_modified ); $month = mysql2date( ‘F’, $single->post_modified ); $date = mysql2date( ‘d’, $single->post_modified ); // specifies the position of the current post $ordered_posts[ $year ][ $month ][ $date ][] = $single; } $current_year = current_time( ‘Y’ ); $current_month = current_time( ‘F’ ); // iterates the years ?> <div class=“menu”> <ul class=“page_item page_item_has_children recent-index-none” > <?php foreach ( $ordered_posts as $year => $months ) { ?> <li> <a href=“javascript:void(0);” title=“Click to show or hide text” class=“list-toggle recent-index-link” > <span class=“recent-index-span” <?php echo ( ( ( $current_year != $year ) ) ? ” : ‘style=“display:none;“ ‘ ); ?>►</span> <span class=“recent-index-span” <?php echo ( ( ( $current_year == $year ) ) ? ” : ‘style=“display:none;“ ‘ ); ?>▼</span> <?php echo $year ?> </a> <div <?php echo ( ( ( $current_year == $year ) ) ? ” : ‘style=“display:none;“ ‘ ); ?> <ul class=“page_item page_item_has_children recent-index-none” > <?php foreach ( $months as $month => $dates ) { // iterates the months ?> <li> <a href=“javascript:void(0);” title=“Click to show or hide text” class=“list-toggle recent-index-link” > <span class=“recent-index-span” <?php echo ( ( ( $current_year != $year ) || ( $current_month != $month ) ) ? ” : ‘style=“display:none;“ ‘ ); ?>►</span> <span class=“recent-index-span” <?php echo ( ( ( $current_year == $year ) && ( $current_month == $month ) ) ? ” : ‘style=“display:none;“ ‘ ); ?>▼</span> <?php echo $month ?> </a> <div <?php echo ( ( ( $current_year == $year ) && ( $current_month == $month ) ) ? ” : ‘style=“display:none;“ ‘ ); ?> <?php foreach ( $dates as $date => $posts ) { // iterates the dates ?> <ul class=“page_item page_item_has_children”> <li> <?php echo $month . ‘ ’ . $date; ?> <ul class=“page_item”> <?php foreach ( $posts as $single ) { // iterates the posts echo ‘<li>’; echo ( ( ‘post’ === $single->post_type ) ? ‘Post ’ : ‘Page ’ ); echo ‘<a class=“recent-index-link” href=“ ‘ . get_permalink( $single->ID ) . ’ ”>’; echo get_the_title( $single->ID ); echo ‘</a>’; if ( $single->post_date === $single->post_modified ) { echo ’ published at ’ . mysql2date( ‘g:i a’, $single->post_modified ) . ‘.’; } else { echo ’ updated at ’ . mysql2date( ‘g:i a’, $single->post_modified ) . ‘,’; echo ’ originally published on ’ . mysql2date( ‘F d, Y’, $single->post_date ) . ’ at ’ . mysql2date( ‘g:i a’, $single->post_date ) . ‘.’; } echo ‘</li>’; } ?> </ul> <!– ul.posts –> </li> </ul> <!– ul.dates –> <?php } // ends foreach $dates ?> </div> </li> <?php } ?> </ul> <!– ul.months –> </div> </li> <?php } // ends foreach for $months ?> </ul> <!– ul.years –> </div> <?php } |
The short URL of the present article is: http://www.terryobrien.me/No6nS
Personal Links