Skip to content

Instantly share code, notes, and snippets.

@graylaurenm
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graylaurenm/aa9dade36cf61de24fd2 to your computer and use it in GitHub Desktop.
Save graylaurenm/aa9dade36cf61de24fd2 to your computer and use it in GitHub Desktop.
List Posts By Category: Custom page template to grab all categories and create an un-ordered list of posts within each. Page template create for Genesis, but could be adapted to any WordPress theme. http://oncecoupled.com/2014/06/13/page-template-list-posts-by-category/
<?php
/**
* Template Name: Category Index
* Created By: Lauren Gray Designs
* URL: http://oncecoupled.com
*/
/* Exclude categories: */
$args = array(
'type' => 'post',
'orderby' => 'name',
'exclude' => array(3,52,125,133,663,745),
);
<?php
/**
* Template Name: Category Index
* Created By: Lauren Gray Designs
* URL: http://oncecoupled.com
*/
// Remove the loop and replace it with our own
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'LGD_add_archive', 9 );
function LGD_add_archive() {
$args = array(
'type' => 'post',
'orderby' => 'name'
);
$categories = get_categories($args);
// List each category
foreach($categories as $category) {
// Output the cateogry (and link to it)
echo '<ul><li><h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></h3><ul>';
$args = array(
'posts_per_page' => -1,
'category' => $category->term_id );
$myposts = get_posts( $args );
// List the posts in said category
foreach ( $myposts as $post ) : setup_postdata( $post );
echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
endforeach;
// Close everything up
echo '</li></ul></ul><br><br>';
}
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment