Custom Genesis Loop on Blog Archive Page

To customise the Genesis loop on the blog page only, the blog page can be conditionally selected,  the loop removed on that page only and a custom loop inserted.

The blog page is selected with is_home, the genesis_do_loop is removed and a new loop wpb_change_home_loop is inserted.

<?php
//do not add in opening php tag
/**
* Custom Genesis Home Loop with Character Limitation on Excerpt
*
* @package Custom Genesis Home Loop with Character Limitation on Excerpt
* @author Neil Gee
* @link https://wpbeaches.com/custom-genesis-standard-loop-blog-page/
* @copyright (c)2014, Neil Gee
*/
add_action('genesis_before_loop', 'wpb_change_home_loop');
/*
* Adding in our new home loop.
*/
function wpb_change_home_loop() {
if ( is_home() ) {
/** Replace the home loop with our custom **/
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'wpb_custom_loop' );
/** Custom loop **/
function wpb_custom_loop() {
if ( have_posts() ) :
do_action( 'genesis_before_while' );
while ( have_posts() ) : the_post();
do_action( 'genesis_before_entry' );
printf( '<article %s>', genesis_attr( 'entry' ) );
do_action( 'genesis_entry_header' );
do_action( 'genesis_before_entry_content' );
printf( '<div %s>', genesis_attr( 'entry-content' ) );
//do_action( 'genesis_entry_content' ); //Remove standard excerpt
echo genesis_do_post_image(); //Add in featured image
echo the_excerpt_max_charlength(200); //change amount of characters to display
echo '</div>';
do_action( 'genesis_after_entry_content' );
do_action( 'genesis_entry_footer' );
echo '</article>';
do_action( 'genesis_after_entry' );
endwhile; //* end of one post
do_action( 'genesis_after_endwhile' );
else : //* if no posts exist
do_action( 'genesis_loop_else' );
endif; //* end loop
}
}
}
/*
* Limit the excerpt by character.
*
* @link Reference - http://codex.wordpress.org/Function_Reference/get_the_excerpt
*/
function the_excerpt_max_charlength($charlength) {
$excerpt = get_the_excerpt();
$charlength++;
if ( mb_strlen( $excerpt ) > $charlength ) {
$subex = mb_substr( $excerpt, 0, $charlength - 5 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
echo mb_substr( $subex, 0, $excut );
} else {
echo $subex;
}
echo ' <br><a href="' . get_permalink() . '" class="more-link" title="Read More">Read More</a>';
} else {
echo $excerpt;
}
}
view raw homeloop.php hosted with ❤ by GitHub

The core markup for the genesis_do_loop is found in the framework at /lib/structure/loops.php line 75 genesis_standard_loop, so here you can adjust what is needed to output.

In the example above the excerpt is removed genesis_entry_content, but re-added with the_excerpt_max_charlength which refers to a separate function below the loop function to alter the character count of the excerpt – set to 200 in the example, also since the featured image is removed by genesis_entry_content this is also added back in with genesis_do_post_image.

4 Comments

  1. Paul Lacey on September 23, 2016 at 11:28 pm

    Thanks man… This is the explanation that has really helped me. Now almost anything seems possible.

    This is a great tutorial for replacing basically any core Genesis function too.

    Cheers

    • Neil Gowran on September 24, 2016 at 6:44 am

      No worries, yes you can then basically custom code any loop you need.

  2. Emman The GreaT on April 12, 2016 at 7:44 am

    Thanks, it helps me alot…

  3. Duy nguyen on September 9, 2015 at 2:58 am

    Thank you! this is a very helpful article for me.

Leave a Comment





%d bloggers like this: