0

WordPress Version: 3.3.2

Assumption, My post in category 7 have 9 posts during October 2012.

by accessing

www.example.com/2012/10/?cat=7

I am able to view all in one page. No issue, however, I create custom page category-7.php in theme folder.

//listed category per page = 6
$query = 'posts_per_page=6&cat=7';
$year = get_query_var('year');
if($year > 0)
{
    //build query string
    $query .= '&year='.$year;
}
$month = get_query_var('monthnum');
if($month > 0)
{
    $query .= '&monthnum='.$month;
}
$paged = get_query_var('paged');
if($paged > 0)
{
    $query .= '&paged='.$paged;
}
query_posts($query);

Now I access again

www.example.com/2012/10/?cat=7

The bottom got Next Page option, but when I click it to

www.example.com/2012/10/page/2/?cat=7

I got "404 - Page not found!"

The only solution I can do is go to WP-Admin, Reading Settings change the "Blog pages show at most" variable to 6, then I got the correct output.

However, as I research in the wordpress page, by added query_posts parameter, it should be work correctly.

Please advise. Thanks!

1 Answer 1

1

The problem is that you're stomping the main query by using query_posts(). Don't do that.

Instead, filter the main query via pre_get_posts:

function wpse82113_filter_pre_get_posts( $query ) {
    if ( is_category( 7 ) && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '6' );
    }
}
add_action( 'pre_get_posts', 'wpse82113_filter_pre_get_posts' );
6
  • Hi, this code is required add to functions.php, as I am using premium wordpress theme, it will affect when I do the theme upgrade. Is it possible way I can do in category-7.php, so it is independence?
    – Shiro
    Commented Jan 21, 2013 at 1:31
  • 1
    Using a commercial Theme or not should make no difference. You can put this in a Child Theme functions.php file, or you can use a custom, site-functionality Plugin, and put it there. But a pre_get_posts filter callback should never be put in the template file, because by the time the template is loaded, the query has already been run, and the posts have already been fetched. Commented Jan 21, 2013 at 1:35
  • Thanks Chip Bennett, I learn a lot from you. As you said never put in the template file, but in the beginner you mention can put in child Theme file, so which one is better? I am still new in WordPress, do you have any more tutorial or learning site regarding this? Thanks!
    – Shiro
    Commented Jan 21, 2013 at 1:52
  • 1
    In a Child Theme, you still wouldn't put it in the template file, but rather in the Child Theme's functions.php file. But I would go with a site-functionality Plugin as the better solution. Here's a tutorial for creating a site functionality Plugin. Commented Jan 21, 2013 at 2:38
  • 1
    Usually, when referring to WordPress Themes, a template file is a file used to render the front-end output, as found in the Template Hierarchy. Anything else is either a template-part file (called from within a template file), or else a functional file (functions.php, or any file included in functions.php, that defines Theme functions/functionality). Commented Jan 21, 2013 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.