Zayd Bhyat
Zayd Bhyat

Reputation: 101

Pagination Solutions for multiple queries with multiple tabs Wordpress

Hey guys so I have a projects page on our website that utilizes tabs to separate multiple different project types across different tabs. Each tab runs its own query by category, ordering the projects in that category by date and displaying: The title, a date, views, author, a small extract of the post and a featured image.

The problem now is that I have too many posts per tab and you have to scroll down a long way to see older projects. What is the best solution for breaking each project post by category into pages of 4 posts. Is a better solution to run a single loop, separate the projects into other tabs by category and the paginate the page? Or is there a more eloquent solution?

Here is my main Project page with tabs:

          <div id="categoryTabs">
            <ul id="news_style1_tabs" class="tabs">
                <li><a href="#mining">Mining</a></li>
                <li><a href="#ong">Oil & Gas</a></li>
                <li><a href="#energy">Energy</a></li>
                <li><a href="#infs">Infastructure</a></li>
                <li><a href="#global">Global</a></li>
            </ul>

<section id="news_style1_body" class="news_body"> 

<div class="tab_all" id="mining">
<?php get_template_part('page-projectsTabs-mining'); ?>
</div>

<div class="tab_all" id="ong">
<?php get_template_part('page-projectsTabs-ong'); ?>
</div>

 <div class="tab_all" id="energy">
<?php get_template_part('page-projectsTabs-energy'); ?>
</div>

 <div class="tab_all" id="infs">
<?php get_template_part('page-projectsTabs-infs'); ?>
</div>
 <div class="tab_all" id="global">
<?php get_template_part('page-projectsTabs-glob'); ?>
</div>
</section>

and here is an example of one of the tabs with a category

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_mining = array(   'post_type'     => 'projects', 
    'status'        => 'published', 
    'category_name'     => 'Mining',
    'posts_per_page'=> 4,
    'orderby'   => 'post_date',
    'order'         => 'DESC'
);
$get_mining = new WP_Query($args_mining);
if($get_mining->have_posts()){
    while($get_mining->have_posts()) : $get_mining->the_post(); 
    $image = wp_get_attachment_image(get_post_thumbnail_id($get_mining->ID),'home-feature');?>

                <article>
                  <div class="pic"><a href="<?php the_permalink();?>" class="w_hover img-link img-wrap"><?php echo $image; ?><span class="link-gallery"></span> </a> </div>
                  <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                  <ul class="icons">
                    <li><a href="<?php the_permalink(); ?>" class="post_date"><?php echo get_the_time('d M, Y', $get_mining);?></a></li>
                    <li><a href="<?php the_permalink(); ?>" class="post_views"><?php echo get_post_meta(get_the_ID(),'views',true);?></a></li>
                    <li><a href="<?php the_permalink(); ?>" class="post_submitted"><?php echo get_post_meta(get_the_ID(),'Author',true);?></a></li>
                    <li><a href="" class="comments_count"></a></li>
                  </ul>
                   <div class="text"><?php echo get_excerpt(900,$get_mining); ?></div>
                </article>

<?php endwhile;
}?>
<?php wp_reset_postdata(); ?>

Any help and advice will be good. Please note at the moment I have about 5 tabs

EDIT 1: Added my Pagination Function in my functions.php

Below is the code for pagination found in my functions.php

 function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
            $numpages = 1;
        }
}
    //Check page number and store $pageUp and $pageDown appropriately
  if($paged == '1'){
    $pageDown = $paged + 1;
    $pageUp = $paged +2;
    $prev = get_previous_posts_page_link($pageDown);
    $next = get_next_posts_page_link($pageUp);


  }
  elseif(($paged >= 2)&&(!$paged==$numpages)){
    $pageDown = $paged -1;
    $pagedUp = $paged +1;
    $prev = get_previous_posts_page_link();
    $next = get_next_posts_page_link();
  }
  elseif($paged == $numpages){
    $pageDown = $paged -2;
    $pageUp = $paged -1;
    $prev = get_previous_posts_page_link($pageDown);
    $next = get_next_posts_page_link($pageUp);
  }  



  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => False,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {

    echo '<div id ="nav_pages">'; 
    echo '<div class="prev_first"></div>'; 
        //echo '   <a href="'.$paginate_links.'" class="next">Next</a>';
        echo '<div class="pages"';
        echo'<ul>';
        if($paged==1){      
            $pageDown = $paged + 1;
            $pageUp = $paged +2;
            $prev = get_next_posts_page_link();
            $next = get_next_posts_page_link();
            echo' <li class="current"><a href="" title="">'.$paged.'</a></li>';
            echo'<li><a href="'.$prev.'" title="'.$prev.'">'.$pageDown.'</a></li>';
            //echo'<li><a href="'.$next.'" title="'.$next.'">'.$pageUp.'</a></li>';
            }
        elseif(($paged >= 2)&&($paged!=$numpages)){
            $pageDown = $paged -1;
            $pageUp = $paged +1;
            $prev = get_previous_posts_page_link();
            $next = get_next_posts_page_link();
            echo'<li><a href="'.$prev.'" title="'.$prev.'">'.$pageDown.'</a></li>';
            echo' <li class="current"><a href="" title="">'.$paged.'</a></li>';
            echo'<li><a href="'.$next.'" title="">'.$pageUp.'</a></li>';
        }

        elseif($paged==$numpages){
            $pageDown = $paged -2;
            $pageUp = $paged -1;
            $prev = get_previous_posts_page_link();
            $next = get_next_posts_page_link();
            echo'<li><a href="'.$prev.'" title="'.$prev.'">'.$pageUp.'</a></li>';
            //echo'<li><a href="'.$next.'" title="'.$next.'">'.$pageUp.'</a></li>';
            echo' <li class="current"><a href="" title="">'.$paged.'</a></li>';


        }   
         //echo $paginate_links;
         echo '</ul>';
          echo '<div class="page_x_of_y">Page <span>' . $paged . '</span> of <span>' . $numpages . '</span></div> ';

        echo '</div>';
  } 

}

//End of pagination

Upvotes: 0

Views: 1845

Answers (1)

raju_odi
raju_odi

Reputation: 1475

use below code for paged :

$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;

and use it in query like below :

$args_mining = array(   'post_type'     => 'projects', 
    'status'        => 'published', 
    'category_name'     => 'Mining',
    'posts_per_page'=> 4,
    'orderby'   => 'post_date',
    'order'         => 'DESC',
    'paged'    => $paged1
);

then for pagination use below code after endwhile;

$pag_args1 = array(
'prev_text'    => __('<'),
'next_text'    => __('>'),
'show_all' => true,
'base'         => '' . $url . '?paged1=%#%',                
    'format'  => '?paged1=%#%',
    'current' => $paged1,
    'total'   => $get_mining->max_num_pages); 
    echo paginate_links( $pag_args1 );

Note: (use $paged1 and $args_mining as different variable for different query for categotry )

Upvotes: 1

Related Questions