Skip to content

Instantly share code, notes, and snippets.

@danbru1989
Last active February 28, 2018 00:40
Show Gist options
  • Save danbru1989/85f342767bf41456570795b36ef59a85 to your computer and use it in GitHub Desktop.
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.
<?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();
}
@danbru1989
Copy link
Author

danbru1989 commented Feb 28, 2018

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/


// Remove header
remove_action( 'genesis_header', 'genesis_header_markup_open', 5 );
remove_action( 'genesis_header', 'genesis_do_header' );
remove_action( 'genesis_header', 'genesis_header_markup_close', 15 );

// 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 Loop Functions ========== */

// Build custom blog loop that excludes the post from the featured post section
function bds_do_custom_loop() {

  // Only show these sections if displaying page #1
  if ( ! is_paged() ) {
    // Add sections above the main blog loop
    bds_featured_post();
    bds_meeting_info();
  }

  // Run a query to get the ID of the latest featured post
  $exclude_posts_query = new WP_Query( 'cat=1&posts_per_page=1' );
  if ( $exclude_posts_query -> have_posts() ) {
    while ( $exclude_posts_query -> have_posts() ) {
      $exclude_posts_query -> the_post();
      $exclude_post_id = get_the_id();
    }
  }
  wp_reset_postdata();

  // Setup the custom loop
  global $paged;

  $args = array(
    'post_type' => 'post',
    'post__not_in'  => array( $exclude_post_id ),
    'posts_per_page'=> get_option('posts_per_page'),
    'paged'         => $paged,
  );

    // Get correct featured image size for custem blog loop section
    add_filter( 'genesis_pre_get_option_image_size', 'bds_blog_posts_image_size' );

    // Customize the entry meta in the entry header
    add_filter( 'genesis_post_info', 'bds_blog_posts_entry_meta' );
    function bds_blog_posts_entry_meta($post_info) {
      $post_info = '[post_date] [post_comments]';
      return $post_info;
    }

  // Output the custom loop
  echo '<div class="blog-posts-wrapper">';
    genesis_custom_loop( $args );
  echo '</div>';

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment