Custom WP Loop - Output a Category Image and Its 3 Recent Posts alongside it
<?php
/*
Template Name: Cat Blog Page Tester Refactor
*/
# Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
add_filter( 'body_class', 'categories_grid_body_class_refactor' );
/**
* Adds a css class to the body element
*
* @param array $classes the current body classes
* @return array $classes modified classes
*/
function categories_grid_body_class_refactor( $classes ) {
$classes[] = 'categories-grid';
return $classes;
}
# Remove the post content
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
# Add custom post content
add_action( 'genesis_entry_content', 'wpb_do_post_content_refactor' );
/**
* Outputs custom post content
*
* @return void
*/
function wpb_do_post_content_refactor() {
$i = 0;
$columns = 3; // Set the number of columns here (2 to 6)
$column_classes = array( '', '', 'one-half', 'one-third', 'one-fourth', 'one-fifth', 'one-sixth' );
$class = $column_classes[$columns];
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( array(
'include' => array(365,369,376,407,474,475),
));
foreach ( $categories as $category ) {
if (z_taxonomy_image_url($category->term_id)) { // if category image exists
if ($i % $columns == 0) {
echo '<div class="'. $class . ' category-item first">';
}
else {
echo '<div class="' . $class . ' category-item">';
}
// Category title
echo '';
// Category image linking to category archive
echo '<a href="' . get_category_link( $category->term_id ) . '"><img src="'. z_taxonomy_image_url($category->term_id, 'cat-image-small') . '" /><h2 class="category-title">' . $category->name . '</h2></a>';
echo '</div>';
$i++;
}
}
}
add_action( 'genesis_entry_content', 'ludwina_blog_loop_stories_refactor' );
function ludwina_blog_loop_stories_refactor() {
$categories = get_categories( array(
'include' => array(365,369,376,407,474,475),
));
foreach ( $categories as $category ) {
?>
<div class="blog-row-container clearfix">
<div class="large-cat-holder">
<a href="<?php echo $category->name; ?>">
<h2 class="category-title"><?php echo $category->name; ?></h2>
<img src="<?php echo z_taxonomy_image_url( $category->term_id, 'cat-image-large'); ?>" />
</a>
</div>
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'stories', //change per Cat
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
// have_posts() is a wrapper function for $wp_query->have_posts(). Since we
// don't want to use $wp_query, use our custom variable instead.
if ( $the_query->have_posts() ) :
//output the widget mark up
echo '<div class="featured-content featuredpost featured-blog">';
//custom loop
while ( $the_query->have_posts() ) : $the_query->the_post();
printf( '<article %s>', genesis_attr( 'entry' ) );
echo '<a href="' . get_the_permalink() . '" rel="bookmark" title="' . get_the_title() . '">';
the_post_thumbnail('featured-blog'); //Add in featured image
echo '<h3>' . get_the_title() . '</h3>';
echo '<small class="time-blog">' . get_the_time('d/m/Y') . '</small><small class="comment-blog">';
comments_number(); // get_comments_number returns only a numeric value
echo '</small>';
echo '</a>';
printf( '</article>');
endwhile;
endif;
wp_reset_postdata(); //resets loop
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . $category->name . '"><button class="more-blog more-link">More</button></a>';
echo '</div></div>';
}
}
add_action( 'genesis_entry_content', 'wpb_blog_loop_main_refactor' );
function wpb_blog_loop_main_refactor() {
?>
<div class="blog-row-container all-blog-posts-container clearfix">
<h2 class="category-title all-blog-title">Latest Posts</h2>
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
// have_posts() is a wrapper function for $wp_query->have_posts(). Since we
// don't want to use $wp_query, use our custom variable instead.
if ( $the_query->have_posts() ) :
//output the widget mark up
echo '<div class="featured-content featuredpost featured-blog">';
//custom loop
while ( $the_query->have_posts() ) : $the_query->the_post();
printf( '<article %s>', genesis_attr( 'entry' ) );
the_post_thumbnail('highlighted-post'); //Add in featured image
echo '<h3>' . get_the_title() . '</h3>';
the_excerpt();
printf( '</article>');
endwhile;
endif;
wp_reset_postdata(); //resets loop
}
genesis();