Created
January 8, 2016 21:50
-
-
Save rfmeier/3614971908b7be747a70 to your computer and use it in GitHub Desktop.
Use the custom archive setting to modify the number of posts on a custom post type archive page.
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 | |
/** | |
* Use the custom archive setting to modify the number of posts on a custom post type archive page. | |
*/ | |
add_action( 'pre_get_posts', 'cpt_as_pre_get_posts' ); | |
/** | |
* Callback for WordPress 'pre_get_posts' action. | |
* | |
* Modify the number of posts on an testimony archive page if set to a custom value. | |
* | |
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts | |
* | |
* @since 0.0.1 | |
* | |
* @return void | |
*/ | |
function cpt_as_pre_get_posts( $query ) { | |
//* if admin or not on the testimony archive page, exit | |
if ( is_admin() || ! is_post_type_archive( 'testimony' ) ) { | |
return; | |
} | |
//* get the custom posts_per_page value | |
$posts_per_page = (int) genesis_get_cpt_option( 'posts_per_page', 'testimony' ); | |
//* if a valid posts_per_page value and on the main query, adjust the query | |
if ( $posts_per_page && $query->is_main_query() ) { | |
$query->set( 'posts_per_page', $posts_per_page ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment