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; | |
} | |
} |
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.
5 comments
Mansoorkhan TK
Thank you.
Paul Lacey
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
No worries, yes you can then basically custom code any loop you need.
Emman The GreaT
Thanks, it helps me alot…
Duy nguyen
Thank you! this is a very helpful article for me.