Last active
August 29, 2015 14:02
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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