Instantly share code, notes, and snippets.

Embed
What would you like to do?
List Posts by Category in Genesis for WordPress - See tutorial for implementation: https://carriedils.com/create-page-list-posts-by-category-genesis-tutorial/
<?php
/**
* Template Name: Category Archives
*/
add_action( 'genesis_loop', 'custom_category_loop' );
/**
* Custom loop that display a list of categories with corresponding posts.
*/
function custom_category_loop() {
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'orderby' => 'term_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><a href="<?php the_permalink();?>"><?php the_title(); ?></a></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
}
// Start the engine.
genesis();
<?php // remove this line
// Remove the standard loop
remove_action( 'genesis_loop', 'genesis_do_loop' );
<?php //remove this line
<ul> // Start your unordered list
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>
<?php
} // End while
} // End if
</ul> // End your unordered list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment