• the problem is quite simple:

    I have pagination for the archives pages which works fine IF the archive page shows only one category.
    If the page is showing posts from more than one category then paging fails and gives back error such as “nothing found” starting from th second page ./page/2

    For paging I use this function which also shows numbers/links between prev and next button;

    [Code moderated as per the Forum Rules. Please use the pastebin]

    In oreder to recall the function in the loop:

    <?php if (function_exists("pagination")) {
        pagination($additional_loop->max_num_pages);
    } ?>

    The query for the page is:

    <?php if (is_category('notizie')) { ?>
    <?php query_posts('cat=42,55,82'); ?>
    <?php } elseif (is_category('alpinismo')) { ?>
    <?php global $query_string; query_posts($query_string); ?>
    <?php } elseif (is_category('freeride')) { ?>
    <?php global $query_string; query_posts($query_string); ?>
    <?php } else { ?><?php } ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    For category “notizie” it shows posts from cat ID 42,55,82 nut paging is not working. Is I set the query to show only cat ID 42 for category “notizie” (1 category instead of 3), paging is working fine….

    Any idea on how to let the paging work in pages showing posts from more than 1 category?

    many thanks in advance…

Viewing 9 replies - 1 through 9 (of 9 total)
  • I suspect that you need to include the categories for 55 and 82 in your is_category() test:

    <?php if (is_category('notizie') || is_category('name-for-55') || is_category('name-for-82')) { ?>
    Thread Starter tctc

    (@tctc)

    hi vtxyzzy and thanks for your support!

    I have a big problem with pagination that I’m not sure it can be resolved…
    Pagination on my website is working properly (we made it working together some days ago!) but now I have a problem with mother category and its child.
    I explain: I’ve got category Magazine which is NOT mother of B,C,D,E but I tell it to displays all posts of mother category (ID=61)
    Pagination for child categories is working fine but if I display all posts of mother category using the conditional tag is_category , then pagination is not working…
    I have one template file where I create the query using the is_category tag:

    <?php if (is_category('magazine')) { ?>
    <h1>...do things...</h1><p>...do things...</p>
    <?php query_posts('cat=61'); ?>; ?>
    <?php } elseif (is_category('board-sport')) { ?>
    <h1>...do things...</h1><p>...do things...</p>
    <?php global $query_string; query_posts($query_string); ?>
    <?php } else { ?><?php } ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    For pagination I use the following function:

    function pagination($pages = '10', $range = 3)
    {
         $showitems = ($range * 2)+1;
         global $paged;
         if(empty($paged)) $paged = 1;
         if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }
         if(1 != $pages)
         {
             echo "<div class=\"pagination\"><span class=\"pagbg\">Pagina ".$paged." di ".$pages."</span>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>?</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>? Successivi</a>";
             for ($i=1; $i <= $pages; $i++)
             {
                 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Precedenti ?</a>";
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>?</a>";
             echo "</div>\n";
         }
    }

    which I recall using:

    <?php if (function_exists("pagination")) {
        pagination($additional_loop->max_num_pages);
    } ?>

    Any idea on how to create the correct query for pagination to work for mother category and using is_category ?

    Thanks a lot as usual!

    Unfortunately, your code does not make sense to me. Here is how I read it:

    IF the query is category 'magazine' (61)
       <h1> ... do things ...</h1><p> ... do things </p>
       repeat the category=61 query
    ELSE IF the query is board-sport
       <h1> ... do things ...</h1><p> ... do things </p>
       repeat the board-sport query
    ELSE
       do nothing
    ENDIF

    You are apparently just repeating existing queries.

    Also, as I said before, pagination($additional_loop->max_num_pages) is NOT CORRECT!!! $additional_loop is not set anywhere!!

    We did not solve your other pagination problem. You found a way around the problem, but as I said in that post, the problem was still there.

    Thread Starter tctc

    (@tctc)

    hi vtxyzzy, I try to summarize the problem trying to be as precise as I can….

    I have category MAG which is mother of 11 sub-categories. I use permalink and I have a function which delete /category/ from the permalink.
    I have also pages under the MAG section. Thus, for the permalink /MAG/ I have both categories and pages…

    For styling /MAG/ sub-categories I have a file called template-mag.php which gives style to every section depending on the sub-category name.
    This page displays the list of posts belonging to the sub-categories and that we made working with pagination last time.

    Here is the code which displays posts for the sub-categories:

    <?php if (is_category('magazine')) { ?>
    <h1>...do things...</h1><p>...do things...</p>
    <?php
      $temp = $wp_query;
      $wp_query= null;
      $wp_query = new WP_Query('cat=61&paged=' . $paged);
    ?>
    <?php } elseif (is_category('board-sport')) { ?>
    <h1>ALTRI BOARD SPORT</h1><p>testo libero statico</p>
    <?php global $query_string; query_posts($query_string); ?>
    <?php } else { ?><?php } ?>
    <?php if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    As you can see, I’d like category MAGAZINE (which is child of category MAG, thus permalink is /mag/magazine/) to displays all the posts belonging to mother category /MAG/ (id=61).
    Please note that I have no posts under category MAG or MAGAZINE. They are all belonging to other sub-categories.

    Whit the query I posted, if I go to MAG/MAGAZINE/ I can see 10 posts extracted from /MAG/ mother category but pagination is not working… It gives me back this:

    <?php else: ?>
    <h2><?php _e('Nessun articolo presente'); ?></h2>
    <p class="center"><?php _e('Nessun articolo presente'); ?></p>
    <p><?php get_search_form(); ?></p>
    <?php endif; ?>

    I need to write a query which should do that:

    1) Sub-category MAGAZINE displays all the posts belonging to mother category MAG (even if no posts belongs directly to cat MAGAZINE or MAG) – i solved this problem using the query $wp_query = new WP_Query('cat=61&paged=' . $paged);
    Then, pagination also should work but actually is not…

    The rest of the code of the page is taken from https://www.wplover.com/756/how-to-get-custom-wp_query-loop-working-with-pagination-and-wp-pagenavi

    Any suggestion is really much appreciated! Thanks a lot!

    Thread Starter tctc

    (@tctc)

    Please also note that if I delete $additional_loop->max_num_pages from the pagination function call, it does not work anymore for last set…

    I don’t see where the $paged variable is being set. Try setting it like this:

    <?php if (is_category('magazine')) { ?>
    <h1>...do things...</h1><p>...do things...</p>
    <?php
      $temp = $wp_query;
      $wp_query= null;
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $wp_query = new WP_Query('cat=61&paged=' . $paged);
    ?>
    <?php } elseif (is_category('board-sport')) { ?>
    . . .

    The reason that $additional_loop seems to work is that the pagination function has this code in it:

    if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }

    If the $pages variable (which comes from $additional_loop->max_num_pages) is not set, then get $wp_query->max_num_pages.

    If I remember correctly from the other post, the posts_per_page was always 20 even when a different number was used in the query. Something is forcing that number. Until you find where that is happening, you will never be able to completely control the paging.

    Thread Starter tctc

    (@tctc)

    Just tried the new query but I get the error:

    Fatal error: Call to a member function get() on a non-object in /web/htdocs/www.snowboarditaliamag.it/home/wp-includes/query.php on line 27

    which refers to query.php:

    function get_query_var($var) {
    	global $wp_query;
    
    	return $wp_query->get($var);
    }

    maybe something is missing in the query?
    Regarding the pagination problem of the other post, we sorted it out just changing in wp-admin > settings > reading > blog pages show max 10 posts !!

    thank you for helping….

    Sorry, I don’t know what else to try. Hope you find a solution from someone else.

    Thread Starter tctc

    (@tctc)

    Ok… anyway I can try different things… for example I can set a template-magazine.php page which shows all posts and where pagination is working. Then, for the other sub-categories I can use template-mag.php

    Thank you very much anyway!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘paging more categories’ is closed to new replies.