Page Content with Gird Blog Posts

custom-template

This is a custom template. At top displaying the page content and below page content showing the latest blog posts in grid style.

Create a file “template_custom.php” and add the following code. Now upload the file in your child theme folder.

<?php
/*
Template Name: Static Content with Blog Posts
*/

// Fix for the WordPress 3.0 "paged" bug.
$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
$paged = intval( $paged );

if( $paged > 1 ){
  remove_action('genesis_loop', 'genesis_do_loop');
}

// Add your grid loop
add_action( 'genesis_after_loop', 'gd_custom_grid_loop_helper' );
remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' ); 
add_action( 'genesis_after_endwhile', 'genesis_custom_tpl_posts_nav' );

/** Add support for Genesis Grid Loop **/
function gd_custom_grid_loop_helper() {
  global $paged;
  $first_page_limit = 4
  $post_per_page = $next_page_limit = 10; 
 
  if ( function_exists( 'genesis_custom_loop' ) ) {
     if( $paged == 1 )
       $post_per_page = $first_page_limit;
    
    $args = array( 'post_type' => 'post', 'showposts' => $post_per_page, 'paged' => $paged);
    
    if( $paged > 1 )
      $offset = ( $next_page_limit * ( $paged - 2 ) + $first_page_limit );
      
    if( intval( $offset ) > 0 ) $args['offset'] = $offset;
    
    //* Add even/odd post class
    add_filter( 'post_class', 'gd_grid_post_class' );
      
    genesis_custom_loop($args);
  }else {
    //* Add even/odd post class
    add_filter( 'post_class', 'gd_grid_post_class' );
    genesis_standard_loop();
  }
  
  remove_filter( 'post_class', 'gd_grid_post_class' ); 
}

function gd_grid_post_class( $classes ) {
    
	global $wp_query;
        $classes[] = 'one-half';
  
	$classes[] = ($wp_query->current_post % 2 == 0) ? 'first' : '';
  
	return $classes;

}

function genesis_custom_tpl_posts_nav(){
  if ( 'numeric' === genesis_get_option( 'posts_nav' ) )
		genesis_custom_tpl_numeric_posts_nav();
	else
		genesis_prev_next_posts_nav();
}

function genesis_custom_tpl_numeric_posts_nav() {

	if( is_singular() )
		return;

	global $wp_query, $paged;

	//* Stop execution if there's only 1 page
	if( $wp_query->max_num_pages <= 1 )
		return;
  
	$max = intval( $wp_query->max_num_pages );
  
  if( $paged == 1 ):
   $found_posts = $wp_query->found_posts - 4;
   $max = $wp_query->max_num_pages = intval( ceil( $found_posts / 10 ) ) + 1;
  endif;

	//* Add current page to the array
	if ( $paged >= 1 )
		$links[] = $paged;

	//* Add the pages around the current page to the array
	if ( $paged >= 3 ) {
		$links[] = $paged - 1;
		$links[] = $paged - 2;
	}

	if ( ( $paged + 2 ) <= $max ) {
		$links[] = $paged + 2;
		$links[] = $paged + 1;
	}

	genesis_markup( array(
		'html5'   => '<div %s>',
		'xhtml'   => '<div class="navigation">',
		'context' => 'archive-pagination',
	) );

	echo '<ul>';

	//* Previous Post Link
	if ( get_previous_posts_link() )
		printf( '<li class="pagination-previous">%s</li>' . "\n", get_previous_posts_link( apply_filters( 'genesis_prev_link_text', '&#x000AB;' . __( 'Previous Page', 'genesis' ) ) ) );

	//* Link to first page, plus ellipses if necessary
	if ( ! in_array( 1, $links ) ) {

		$class = 1 == $paged ? ' class="active"' : '';

		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

		if ( ! in_array( 2, $links ) )
			echo '<li class="pagination-omission">&#x02026;</li>';

	}

	//* Link to current page, plus 2 pages in either direction if necessary
	sort( $links );
	foreach ( (array) $links as $link ) {
		$class = $paged == $link ? ' class="active"' : '';
		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
	}

	//* Link to last page, plus ellipses if necessary
	if ( ! in_array( $max, $links ) ) {

		if ( ! in_array( $max - 1, $links ) )
			echo '<li class="pagination-omission">&#x02026;</li>' . "\n";

		$class = $paged == $max ? ' class="active"' : '';
		printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );

	}

	//* Next Post Link
	if ( get_next_posts_link() )
		printf( '<li class="pagination-next">%s</li>' . "\n", get_next_posts_link( apply_filters( 'genesis_next_link_text', __( 'Next Page', 'genesis' ) . '&#x000BB;' ) ) );

	echo '</ul></div>' . "\n";

}

//* Run the Genesis loop
genesis();

Create a page (Dashboard -> Pages -> Add New ) and select “Static Content with Blog Posts” page template from template drop down. Now publish the page. That’s it.

Share This Article

Subscribe Membership Plan ($9.99/mo)

  • Get full access to all premium tutorials
  • Get Support for the posted content
  • Request for specific how-to tutorials
  • Cancel the subscription at anytime

Reader Interactions

Leave A Reply

Your email address will not be published. Required fields are marked *