Skip to content

Instantly share code, notes, and snippets.

@micalexander
Created June 11, 2014 02:26
Show Gist options
  • Select an option

  • Save micalexander/9ff679d02a172a70ca43 to your computer and use it in GitHub Desktop.

Select an option

Save micalexander/9ff679d02a172a70ca43 to your computer and use it in GitHub Desktop.
wordpress:publish-date-accordion:snippet
<?php
global $post;
$post_backup = $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'post-type-news',
'orderby' => 'date',
'order' => 'DSC'
);
// Get the posts
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
<?php
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('F', $post->post_date);
$day = mysql2date('j', $post->post_date);
$post_by_months[$year][$month][] = array( 'post_date' => mysql2date('m/j/Y', $post->post_date), 'title' => $post->post_title , 'permalink' => get_permalink($post->ID) );
?>
<?php endwhile;?>
<?php foreach ($post_by_months as $year => $months): ?>
<h2><?php echo $year; ?></h2>
<?php foreach ($months as $month => $items): ?>
<div class="month-wrapper">
<h3><?php echo $month; ?></h3>
<?php foreach ($items as $item): ?>
<div class="permalink-wrapper"><a href="<?php echo $item['permalink']; ?>"><?php echo $item['post_date'] ?>:&nbsp;&nbsp;<?php echo wp_trim_words($item['title'], 7) ?></a></div>
<?php endforeach ?>
</div>
<?php endforeach ?>
<?php endforeach ?>
<?php $args = array(
'base' => home_url() . '/news/%_%',
'format' => 'page/%#%/',
'total' => $wp_query->max_num_pages,
'current' => max( 1, $wp_query->query_vars['paged'] ),
'show_all' => true,
//'end_size' => 1,
//'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('&larr; Previous'),
'next_text' => __('Next &rarr;'),
'type' => 'plain',
'add_args' => False,
'add_fragment' => ''
); ?>
<?php echo paginate_links( $args ) ;
endif;
$post = $post_backup;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment