Posts Display Function
The ‘avp_get_posts()’ function gets a list of all posts and sorts them by year, month and day and displays them in that order as hierarchical lists. The function is simply a series of embedded loops for the year, month, day and post lists. As such, it mirrors the Page hierarchical listing displayed beside it. Visually, it is very similar to the way Pages are displayed, which is the reason it was implemented in this manner.
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 | function avp_get_posts() { $all_posts = get_posts( array( ‘posts_per_page’ => -1 ) ); // this variable will contain all the 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_date ); $month = mysql2date( ‘F’, $single->post_date ); $date = mysql2date( ‘d’, $single->post_date ); // specifies the position of the current post $ordered_posts[ $year ][ $month ][ $date ][] = $single; } $current_year = current_time( ‘Y’ ); $current_month = current_time( ‘F’ ); $current_date = current_time( ‘d’ ); // iterates the years ?> <div class=“menu”> <ul class=“page_item page_item_has_children” style=“list-style: none;”> <?php foreach ( $ordered_posts as $year => $months ) { ?> <li> <a href=“javascript:void(0);” style=‘text-decoration: none;’ class=“list-toggle” > <span class=“sitemap-index-span” style=“display:<?php echo ( ( ( $current_year != $year ) ) ? ” : ‘none’ ); ?>;”>►</span> <span class=“sitemap-index-span” style=“display:<?php echo ( ( ( $current_year == $year ) ) ? ” : ‘none’ ); ?>;”>▼</span> </a> <a href=”<?php echo get_year_link( $year ); ?>”><?php echo $year ?></a> <ul class=“page_item page_item_has_children” style=“list-style: none; display:<?php echo ( ( ( $current_year == $year ) ) ? ” : ‘none’ ); ?>;”> <?php foreach ( $months as $month => $dates ) { // iterates the months ?> <li> <a href=“javascript:void(0);” style=‘text-decoration: none;’ class=“list-toggle” > <span class=“sitemap-index-span” style=“display:<?php echo ( ( ( $current_year != $year ) || ( $current_month !== $month ) ) ? ” : ‘none’ ); ?>;”>►</span> <span class=“sitemap-index-span” style=“display:<?php echo ( ( ( $current_year == $year ) && ( $current_month === $month ) ) ? ” : ‘none’ ); ?>;”>▼</span> </a> <a href=”<?php echo get_month_link( $year, $month ); ?>”><?php echo $month ?></a> <?php foreach ( $dates as $date => $posts ) { // iterates the dates ?> <ul class=“page_item page_item_has_children” style=“list-style: none; display:<?php echo ( ( ( $current_year == $year ) && ( $current_month == $month ) ) ? ” : ‘none’ ); ?>;”> <li> <a href=“javascript:void(0);” style=‘text-decoration: none;’ class=“list-toggle” > <span class=“sitemap-index-span” style=“display:<?php echo ( ( ( $current_year != $year ) || ( $current_month != $month ) || ( $current_date != $date ) ) ? ” : ‘none’ ); ?>;”>►</span> <span class=“sitemap-index-span” style=“display:<?php echo ( ( ( $current_year == $year ) && ( $current_month == $month ) && ( $current_date == $date ) ) ? ” : ‘none’ ); ?>;”>▼</span> </a> <a href=”<?php echo get_day_link( $year, $month, $date ); ?>”><?php echo $date ?></a> <ul class=“page_item” style=“display:<?php echo ( ( ( $current_year == $year ) && ( $current_month == $month ) && ( $current_date == $date ) ) ? ” : ‘none’ ); ?>;”> <?php foreach ( $posts as $single ) { // iterates the posts echo ‘<li>’; echo mysql2date( ‘g:i a’, $single->post_date ) . ‘ ’; echo ‘<a href=“ ‘ . get_permalink( $single->ID ) . ’ ”>’; echo get_the_title( $single->ID ); echo ‘</a>’; if ( 0 < $single->comment_count ) { echo ‘ (‘ . $single->comment_count . ’)’; } echo ‘</li>’; } ?> </ul> <!– ul.posts –> </li> </ul> <!– ul.dates –> <?php } // ends foreach $dates ?> </li> <?php } ?> </ul> <!– ul.months –> </li> <?php } // ends foreach for $months ?> </ul> <!– ul.years –> </div> <?php } |
The function is based on an example at WPSnipp, massaged a little to add sorting by the day as well.
The short URL of the present article is: http://www.terryobrien.me/1i3n4
Personal Links