Created
January 26, 2017 14:35
-
-
Save oalansari82/f42e80f433f5bad5d3cd7b416cb09b6e to your computer and use it in GitHub Desktop.
Two Genesis Queries with pagination
This file contains 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_action( 'genesis_after_header', 'io_news_featured_news_post' ); | |
/** | |
* Outputs a custom loop with one features post | |
* | |
* @global mixed $paged current page number if paginated | |
* @return void | |
*/ | |
function io_news_featured_news_post() { | |
$args = (array( | |
'post_type' => 'post', | |
'order' => 'DESC', | |
'orderby' => 'date', | |
'no_found_rows' => true, //removes pagination | |
'posts_per_page' => 1, | |
)); | |
echo '<div class="latest-post">'; // Adds a custom class to this query | |
genesis_custom_loop( $args ); | |
echo '</div>'; | |
} | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
add_action( 'genesis_loop', 'io_news_page_loop' ); | |
/** | |
* Outputs a custom loop | |
* | |
* @global mixed $paged current page number if paginated | |
* @return void | |
*/ | |
function io_news_page_loop() { | |
global $paged; | |
// accepts any wp_query args | |
$args = (array( | |
'post_type' => 'post', | |
'order' => 'DESC', | |
'orderby' => 'date', | |
'paged' => get_query_var( 'paged' ), | |
'posts_per_page' => 8, | |
)); | |
// Checks if first page and it applies an offset to it. | |
if(0 == $paged) { | |
// Offset & pagination | |
add_action('pre_get_posts', 'io_query_offset', 1 ); | |
} | |
genesis_custom_loop( $args ); | |
} | |
function io_query_offset(&$query) { | |
//Before anything else, make sure this is the right query... | |
if ( ! $query->is_home() ) { | |
return; | |
} | |
//First, define your desired offset... | |
$offset = 1; | |
//Next, determine how many posts per page you want (we'll use WordPress's settings) | |
$ppp = get_option('posts_per_page'); | |
//Next, detect and handle pagination... | |
if ( $query->is_paged ) { | |
//Manually determine page query offset (offset + current page (minus one) x posts per page) | |
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp ); | |
//Apply adjust page offset | |
$query->set('offset', $page_offset ); | |
} | |
else { | |
//This is the first page. Just use the offset... | |
$query->set('offset',$offset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment