Skip to content

Instantly share code, notes, and snippets.

@jsoningram
Last active January 12, 2016 20:44
Show Gist options
  • Save jsoningram/2eb0b7a7e48df182efd4 to your computer and use it in GitHub Desktop.
Save jsoningram/2eb0b7a7e48df182efd4 to your computer and use it in GitHub Desktop.
<?php
$ar = [];
$args = array(
'orderby' => 'id',
);
$terms = get_terms( 'category', $args );
foreach ( $terms as $term ) :
$ar[$term->name] = $term->term_id;
endforeach;
echo '<div class="posts-by-cat">';
foreach( $ar as $key => $value ) :
$cache_key = 'posts_by_cat_' . $value;
$cache_group = 'posts_by_cat';
$posts_by_cat_query = wp_cache_get( $cache_key, $cache_group );
if ( false === $posts_by_cat_query ) {
$queryargs = array(
'category__in' => $value,
'posts_per_page' => 3,
'order' => 'ASC',
'orderby' => 'date',
'no_found_rows' => true,
'update_post_meta_cache' => false
);
$posts_by_cat_query = new WP_Query( $queryargs );
wp_cache_set( $cache_key, $posts_by_cat_query, $cache_group, 10 * MINUTE_IN_SECONDS );
}
if ( $posts_by_cat_query->have_posts() ) {
echo '<ul class="' . $posts_by_cat_query->query_vars['category_name'] . '-posts">';
echo '<h4 class="cat-posts-heading">'
. '<a href="'. esc_url( get_category_link( $posts_by_cat_query->query_vars['cat'] ) )
.'" title="'. $key .'">'
. $key
. '</a>'
. '</h4>';
while ( $posts_by_cat_query->have_posts() ) {
$posts_by_cat_query->the_post();
echo '<li class="cat-post">'
. '<span>' . get_the_title() . '</span>'
. '<button class="secondary-link read-more"'
. 'data-href="' . esc_url( get_the_permalink() )
. '" data-title="' . get_the_title() . '">'
. '<i class="fa fa-angle-double-right"></i>Read More'
. '</button>'
. '</li>';
}
echo '</ul>';
} else {
// no posts found
}
wp_reset_postdata();
endforeach;
echo '</div>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment