• Hi everyone,

    I am developing a site using a child of twentyeleven, which use a custom post type called ‘product’. The site has a category named ‘Products’ which has many child categories such as Product1, Product2 etc.

    I have included a ‘Products’ menu item. When clicked, a page is loaded that lists all the child categories of category ‘Product’, which I have implemented using category-products. When one of these is clicked (as an example ‘Product1’) I want to display all the titles and permalinks of any custom post type ‘product’. Because I use customised php files for everything else, I figured I could use category.php to display these.

    And it works, except for the occasions where the number of products in a child category exceeds the posts_per_page limit. When that happens, the navigation to next page is displayed, but when clicked, it cannot find any posts – displaying the ‘This is somewhat embarrassing isn’t it’ message that is twenty eleven’s default.

    The code for category.php:

    category = get_category( get_query_var( 'cat' ) );
    
    $thiscat = $category->cat_ID;
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array('post_type' => 'product',
    			  'paged' => $paged,
    			  'posts_per_page'=>2,
    			  'cat' => $thiscat );
    
    $wp_query = new WP_Query($args);
    
    if ($wp_query->have_posts () ):
    
    while($wp_query->have_posts() ):
    
    //html to display title and permalink
    
    endwhile
    
    next_posts_link('? Go to next page...');
    previous_posts_link('Go to previous page...');

    For example, if under child category Product1 I have 4 posts, the first two posts display correctly with a ‘Go to next page’ link on this url: ../category/products/product_1/. When this link is pressed I get the ’embarrassing’ message on this url: ../category/products/product_1/page/2/

    Can anyone help me solve this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try changing the use of WP_Query to just query_posts.

    Thread Starter amanojaku2

    (@amanojaku2)

    It then throws an error – call to member function have_posts()on a non object in: if ($wp_query->have_posts () ):

    Moderator keesiemeijer

    (@keesiemeijer)

    If this is on a category archive page (category.php) there is a more reliable way to query from your theme’s functions.php file:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        // all categoy archive pages
        if(is_category()){ // you can also use is_category('products')
          $query->set('posts_per_page', 2);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    And remove the query and use a normal loop in category.php:

    // start of normal loop
    if ( have_posts () ):
    while( have_posts() ):
    //html to display title and permalink
    endwhile
    endif;

    https://www.billerickson.net/customize-the-wordpress-query/

    If this is on a Page template and you’re using query_post you need to use a normal loop like above (without the $wp_query->)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pagination issues with category.php’ is closed to new replies.