Created
January 12, 2014 17:31
-
-
Save alexkingorg/8387783 to your computer and use it in GitHub Desktop.
Customize posts-per-page based on some condition.
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 filter for front-end requests | |
if (!is_admin() && !is_feed()) { | |
add_filter('parse_request', 'my_add_posts_per_page'); | |
} | |
// add our condition-checking filter | |
function my_add_posts_per_page() { | |
add_filter('pre_get_posts', 'my_posts_per_page'); | |
} | |
// customize the posts_per_page, | |
function my_posts_per_page($query) { | |
// we only want our filter to run on the initial query, so we remove it here | |
remove_filter('pre_get_posts', 'my_posts_per_page'); | |
// set your condition - can be whatever you want | |
if (is_category('example')) { | |
// set the number of posts per page | |
$query->set('posts_per_page', 100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment