0

In my custom template files, I create query in the following way:

<?php $query = new WP_Query(array('post_type' => 'aya-bi-aya','author' => $current_user->ID,'paged' => $paged, 'post_status' => array('publish') ) ); ?>

And to perform pagination, I call pagination function after loop like:

<?php my_pagination($query->max_num_pages);?>

But in my taxonomy-xxx.php, when I call pagination the same way, I get the warning:

Notice: Undefined variable: query in C:\xampp\htdocs\balagha-to-alquran\production\wp-content\themes\twentyten\taxonomy-dawahir-balaghia.php on line 186

Notice: Trying to get property of non-object in C:\xampp\htdocs\balagha-to-alquran\production\wp-content\themes\twentyten\taxonomy-dawahir-balaghia.php on line 186

Can you please help me resolve this issue? your help is appreciated.

Edit and solution:

My pagination function:

function my_pagination($pages = '', $range = 2)
{  
     $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'>";

         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</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)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

I resolved the issue by removing the argument from the function call($query->max-num-pages) in my taxonomy-xx.php since the argument is not mandatory. As you can see, there is an if statement inside the function that deals with the absence of argument by calling the global $wp_query. Thank you.

4
  • 1
    Please edit your question to include the full (relevant) code of your taxonomy-xx.php taxonomy template file, as well as the declaration for function my_pagination(). Nov 6, 2013 at 16:25
  • but don't you want the max_num_pages from your $query, and not $wp_query?
    – Milo
    Nov 6, 2013 at 16:34
  • I'm wondering why, if the taxonomy-xx.php template file is being used correctly, pagination isn't working out of the box. I suspect a (n unnecessary) custom query. Nov 6, 2013 at 16:35
  • I really don't fully understand you Milo and Chip Bennett, I am a beginner that still should learn in the Wordpress realm. Nov 7, 2013 at 14:03

1 Answer 1

1

Is the my_pagination() call within the same file as your $query declaration?

If not, you need to put global $query; at the top of your template file where my_pagination() is being called.

If this doesn't solve it, please give some more information/code examples.

4
  • Thank you for your feedback. The second solution unfortunately doesnt work. I will try to call global $query inside of my pagination function and give a feedback here plus some more information Nov 6, 2013 at 16:04
  • Well. Since I didn't create the pagination function myself, I didn't notice that inside, there is possibility to do not give first parameter. In this case, the function automatically uses global wp_query to calculate the max_num_pages. Thank you anyway for your help. Nov 6, 2013 at 16:22
  • 1
    Glad I could be of assistance. It's always recommended to alter $wp_query directly through the pre_get_posts action, if you're interested in best practices. Nov 6, 2013 at 18:05
  • Thank you very much Sir, your advise is invaluable. Good luck Nov 7, 2013 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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