Skip to content

Instantly share code, notes, and snippets.

@kosinix
Last active December 16, 2015 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosinix/5472359 to your computer and use it in GitHub Desktop.
Save kosinix/5472359 to your computer and use it in GitHub Desktop.
Shortcode for displaying list of posts with prev and next links. Place the code in functions.php and use it as follows: [list_posts] or in template files as a PHP code: <?php echo do_shortcode('[list_posts]'); ?>
<?php
function list_posts_shortcode(){
$my_query = new WP_Query(
array(
'post_type' => 'post',
'paged' => get_query_var('paged')
)
);
$html = '<ul class="list-posts">';
while ( $my_query->have_posts() ) : $my_query->the_post();
$title = apply_filters('the_title', get_the_title());
$thumb = get_the_post_thumbnail('full');
$meta = get_post_custom(get_the_ID());
$excerpt = apply_filters('the_excerpt', get_the_excerpt());
$content = apply_filters('the_content', get_the_content());
$permalink = get_permalink();
$html .= '<li class="post-entry clearfix">
<h3 class="entry-title"><a href="'.$permalink.'">'.$title.'</a></h3>
<div class="entry-meta">Posted on '.get_the_time('F d, Y').'</div>
<div class="entry-content">
'.$excerpt.'
</div>
</li>';
endwhile;
$html .= '</ul>
<div class="pagination clearfix">
<div class="prev">'.get_next_posts_link( '&laquo; Older Posts', $my_query->max_num_pages).'</div>
<div class="next">'.get_previous_posts_link( 'Newer Posts &raquo;', $my_query->max_num_pages ).'</div>
</div>';
wp_reset_postdata();
return $html;
}
add_shortcode('list_posts', 'list_posts_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment