Last active
February 28, 2018 00:40
-
-
Save danbru1989/85f342767bf41456570795b36ef59a85 to your computer and use it in GitHub Desktop.
A custom Genesis blog page displaying three main sections. The 1st section is a featured post from category #1. The 2nd section shows some meeting info. The 3rd section displays posts from all categories except the post displayed in the 1st section.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add sections above the main blog loop | |
add_action( 'genesis_before_loop', 'bds_featured_post' ); | |
add_action( 'genesis_before_loop', 'bds_meeting_info' ); | |
// Replace the blog loop with custom loop | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
add_action( 'genesis_loop', 'bds_do_custom_loop' ); | |
// Run the Genesis loop. | |
genesis(); | |
/* ========== Build Functions ========== */ | |
// Following instructions for excluding a post already displayed: https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action | |
// Build featured post section | |
function bds_featured_post() { | |
$featured_posts_query = new WP_Query( 'cat=1&posts_per_page=1' ); | |
// Start the loop | |
if ( $featured_posts_query -> have_posts() ) : | |
echo '<div class="latest-update-wrapper">'; | |
do_action( 'genesis_before_while' ); | |
while ( $featured_posts_query -> have_posts() ) : $featured_posts_query -> the_post(); | |
// Create a global variable to use later in the bds_do_custom_loop() | |
global $exclude_post_id; | |
$exclude_post_id = get_the_id(); | |
do_action( 'genesis_before_entry' ); | |
genesis_markup( array( | |
'open' => '<article %s>', | |
'context' => 'entry', | |
) ); | |
do_action( 'genesis_entry_header' ); | |
do_action( 'genesis_before_entry_content' ); | |
printf( '<div %s>', genesis_attr( 'entry-content' ) ); | |
do_action( 'genesis_entry_content' ); | |
echo '</div>'; | |
do_action( 'genesis_after_entry_content' ); | |
do_action( 'genesis_entry_footer' ); | |
genesis_markup( array( | |
'close' => '</article>', | |
'context' => 'entry', | |
) ); | |
do_action( 'genesis_after_entry' ); | |
endwhile; | |
do_action( 'genesis_after_while' ); | |
else : // If no posts exist. | |
do_action( 'genesis_loop_else' ); | |
echo '</div>'; | |
endif; // End loop. | |
wp_reset_postdata(); | |
} | |
// Build meeting information section | |
function bds_meeting_info() {echo "Coming Soon";} | |
// Build custom blog loop that excludes the post from the featured post section | |
function bds_do_custom_loop() { | |
// Get post ID from the post displayed in the Featured Post section | |
global $exclude_post_id; | |
// Start the loop | |
if ( have_posts() ) : | |
do_action( 'genesis_before_while' ); | |
while ( have_posts() ) : the_post(); | |
// Exclude the post from the Featured Post section and continue the loop | |
if ( get_the_id() == $exclude_post_id ) continue; | |
do_action( 'genesis_before_entry' ); | |
genesis_markup( array( | |
'open' => '<article %s>', | |
'context' => 'entry', | |
) ); | |
do_action( 'genesis_entry_header' ); | |
do_action( 'genesis_before_entry_content' ); | |
printf( '<div %s>', genesis_attr( 'entry-content' ) ); | |
do_action( 'genesis_entry_content' ); | |
echo '</div>'; | |
do_action( 'genesis_after_entry_content' ); | |
do_action( 'genesis_entry_footer' ); | |
genesis_markup( array( | |
'close' => '</article>', | |
'context' => 'entry', | |
) ); | |
do_action( 'genesis_after_entry' ); | |
endwhile; | |
do_action( 'genesis_after_while' ); | |
else : // If no posts exist. | |
do_action( 'genesis_loop_else' ); | |
endif; // End loop. | |
wp_reset_postdata(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the tips! Very helpful!
Here's what I ended up doing...
I couldn't use offset because I wanted pagination and I didn't want to exclude just any post. I wanted to be sure that I was excluding the post already displayed in the upper section. Final product: http://brubakerministries.org/updates/