Pagination Solutions for multiple queries with multiple tabs Wordpress - php

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>Mining</li>
<li>Oil & Gas</li>
<li>Energy</li>
<li>Infastructure</li>
<li>Global</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"><?php echo $image; ?><span class="link-gallery"></span> </div>
<h3><?php the_title(); ?></h3>
<ul class="icons">
<li><?php echo get_the_time('d M, Y', $get_mining);?></li>
<li><?php echo get_post_meta(get_the_ID(),'views',true);?></li>
<li><?php echo get_post_meta(get_the_ID(),'Author',true);?></li>
<li></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' => __('«'),
'next_text' => __('»'),
'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 ' Next';
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">'.$paged.'</li>';
echo'<li>'.$pageDown.'</li>';
//echo'<li>'.$pageUp.'</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>'.$pageDown.'</li>';
echo' <li class="current">'.$paged.'</li>';
echo'<li>'.$pageUp.'</li>';
}
elseif($paged==$numpages){
$pageDown = $paged -2;
$pageUp = $paged -1;
$prev = get_previous_posts_page_link();
$next = get_next_posts_page_link();
echo'<li>'.$pageUp.'</li>';
//echo'<li>'.$pageUp.'</li>';
echo' <li class="current">'.$paged.'</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

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 )

Related

Custom Post Pagination Not Working

I know there have been so many pagination questions, but I'm really stumped here. I have a "gallery" hierarchical custom post type. For the parent posts, I'm using an index template, and the child posts uses more typical single page code.
Here's some of the single-gallery.php file. I just check to see if the post has children. If it does, I want it to be an index page.
get_header();
$template = get_template_directory_uri();
//If it's an index page, redirect it
$args = array(
'post_parent' => $post->ID,
'post_type' => 'gallery',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
if ($children){
get_template_part('template-parts/content', 'gallery-index');
get_footer();
} else{
After that else, it outputs all the code for the single display, which is working fine.
In content-gallery-index.php, I have.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
Then it goes on to handle code for another, unrelated page. Lower in the template, I have the following (I included all of it by request):
<?php elseif (is_page(343) || $post_type == 'gallery' ):
$post_parent = 343;
$type = 'page';
if ($post_type == 'gallery'){
$post_parent = $post->ID;
$type = 'gallery';
}
$args = array(
'post_type' => $type,
'post_parent' => $post_parent,
'posts_per_page' => 6,
'paged' => $paged,
);
$query = new WP_Query($args);
if ($query->have_posts()): ?>
<div class="box-steps">
<?php while($query->have_posts()): $query->the_post();
$image = get_the_post_thumbnail();
$permalink = get_the_permalink();
if (!get_field('dont_include_in_services')): ?>
<section class="box-step">
<div class="box-step-img-wrapper">
<a href="<?php echo $permalink; ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<div class="box-step-content-wrapper">
<h2 class="box-step-title our-services-title header-font"><?php echo get_the_title(); ?></h2>
<?php if ($post_type == 'gallery'): ?>
<p class="box-step-content"><?php echo get_the_content($post->ID); ?></p>
<?php else: ?>
<p class="box-step-content"><?php echo esc_html(get_field('preview_text')); ?></p>
<a class="box-step-link block bold" href="<?php echo $permalink; ?>">
READ MORE
</a>
<?php endif; ?>
</div>
</section>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
<!-- pagination here -->
<?php
custom_pagination($query->max_num_pages,"",$paged);
?>
<?php endif; ?>
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;
}
}
$prev_arrow = is_rtl() ? '»' : '«';
$next_arrow = is_rtl() ? '«' : '»';
$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' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list',
'add_args' => false,
'add_fragment' => '',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
//echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
The strange thing is that the numbered pagination is output fine, but when I click on page two, it's like the URL just gets rewritten and the page just refreshes. This same code has worked on another page, but that page is not a custom post type. I've also tried the trick of turning wp_query to null and resetting it, but then I get other errors.

Add pagination to custom search page

I am a debutant in WordPress. I have a custom WordPress search page and I want to add pagination my loop.
<div class="content active" id="panel1">
<?php
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=post&s='.$keyword.'&posts_per_page=5&paged='.$paged.'&cat='.$catSearchID);
//print_r($wp_query);exit();
$current = -1;
$current2 = 0;
$emptyPost2 = 0;
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php endwhile; ?>
</div><!-- END panel1-->
<?php wp_reset_query(); ?>
<?php get_template_part('pagination'); ?>
</div><!-- END tabs-content-->
My template pagination :
global $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query- >query_vars['paged'] : $current = 1;
$t_pagination = array(
'base' => #add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => false,
'type' => 'plain',
);
if($wp_rewrite->using_permalinks())
$t_pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) )) . 'page/%#%/', 'paged');
if(!empty($wp_query->query_vars['s']))
$t_pagination['add_args'] = array('s'=>get_query_var('s'));
echo '<div class="pagination">' . paginate_links($t_pagination) . '</div>';
I tried a lot of solutions but it give in second page 404 error.

Wordpress pagination - Can't go to other pages

I'm using wordpress and I'm trying to create a pagination with wordpress WP_query. I have used a tutorial for this and some people had the same problem like me that they couldn't connect to the second or third page of their pagination. I have 11 posts in my database and I have split them so they show 4 per page and whether I try to go to second or third page, it just says "404 page not found". Here is my index.php:
<?php
get_header();?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'all_products',
'posts_per_page' => 4,
'paged' => $paged,
'page' => $paged
);
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<article class="loop">
<div class="product">
<h1><?php the_title(); ?></h1>
<ul>
<li><strong>Description:</strong> <?php echo(types_render_field( "description_of_the_product", array( 'raw' => true) )); ?></li>
<li><strong>Type:</strong> <?php echo(types_render_field( "type", array( 'raw' => true) )); ?></li>
<li><strong>Price:</strong> <?php echo(types_render_field( "price", array( 'raw' => true) )); ?></li>
<li><strong>Manufacturer:</strong> <?php echo(types_render_field( "manufacturer", array( 'raw' => true) )); ?></li>
<li><strong>Availability:</strong> <?php echo(types_render_field( "availability:", array( 'raw' => true) )); ?></li>
<li><strong>Reference:</strong> <?php echo(types_render_field( "reference", array( 'raw' => true) )); ?></li>
Read more
<ul>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists('custom_pagination')) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
?>
<?php next_posts_link( $label , $max_pages ); ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php get_footer(); ?>
Any help is greatly appreciated.
Pagination function(custom_pagination):
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$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' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}

Wordpress pagination static page

I'm working on a Wordpress template and want to use a static page as homepage. I made a pagination on that page, but when I go to another, the URL changes and also the posts of the other page shows up, but the pagination's current page is the same. It says that it's still on 'Page 1 of ....' in the pagination.
Does someone know how I can change the current page?
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;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $page,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<span class='page-numbers page-num'>Page " . $page . " of " . $numpages . "</span> ";
echo $paginate_links;
}
}
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged
);
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
// Get all posts
get_template_part( 'content', 'post' );
endwhile; ?></div>
<!-- end of the loop -->
<nav class='paging-navigation'>
<?php
if (function_exists(custom_pagination)) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
?></nav>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?></div>
Solved the problem! Put this:
<?php
if (function_exists(custom_pagination)) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
After the wp_reset_postdata, also removed 'page' => $paged from $query_args, this worked for me. :)

WordPress custom post type pagination 404 error

I am trying to get pagination working on a custom theme.
I have a post type with custom taxonomies and I have pagination for those custom taxonomies and that works fine.
However on another part of the site I have another custom post type; I am trying to paginate through the posts in this post type. Currently the posts are in a custom taxonomy also in this post type, the custom taxonomy behaves like a category.
The URL structure is http://www.domain.com/latest-news/, however when I go to http://www.domain.com/latest-news/2/ I get a 404 error, this is the problem.
Here is my code (sorry it's so long):
<?php
$numCats = 0;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$posts_per_page = 5;
$offset = ($posts_per_page * $paged - $posts_per_page);
$taxonomies = array(
'latest-cat'
);
$args = array(
'hide_empty' => false,
'parent' => 0,
'orderby' => 'id',
'order' => 'DESC',
'paged' => $paged
);
$terms = get_terms($taxonomies, $args);
foreach ($terms as $term) {
$args = array(
'post_type' => 'latest_news_item',
'oserby' => 'id',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'latest-cat',
'terms' => $term->term_id
)
)
);
$wp_query = new WP_Query($args);
while ($wp_query->have_posts() ) : $wp_query->the_post();
$numCats++;
endwhile;
$args = array(
'post_type' => 'latest_news_item',
'parent' => 115,
'orderby' => 'id',
'order' => 'ASC',
'posts_per_page' => $posts_per_page,
'offset' => 0,
'paged' => $paged
);
$wp_query = new WP_Query($args);
while ($wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="news-events-item">
<h3><?php echo get_the_title(); ?></h3>
<?php $content = get_the_content(); ?>
<p class="summary"><?php echo wp_trim_words($content, '30', '...'); ?>More</p>
</div>
<?php
endwhile;
} ?>
The $numCats variable is just to count the number of posts so that I can set up the pagination links, and that part works.
Thank you
I have fixed this for myself in this way:
On an archive page of the custom post type "blog".
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=blog&paged=' . $paged );
$args = array(
'post_type' => 'blog',
'paged' => $paged,
'orderby' => 'date',
'order' => 'DEST',
'posts_per_page' => '1',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
this is the classic loop with added arguments for post type "blog" and added "Paged" attribute.
And obviously these points:
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=blog&paged=' . $paged );
At the end, outside of the loop is the pagination output from functions.php
<?php echo pagination($loop->max_num_pages); ?>
In functions.php is the permalink modification and also modification of the "posts_per_page" set to ONE post, so you can test the pagination.
function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => #add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'plain'
);
if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}
function portfolio_posts_per_page( $query ) {
if ( $query->query_vars['post_type'] == 'blog' ) $query->query_vars['posts_per_page'] = 1;
return $query;
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page' );
And as last here is the pagination code which is used on the archive page outside of the loop.
**used with bootstrap 5.0**
function pagination($pages = '', $range = 1)
{
$showitems = ($range * 2) + 1;
global $paged;
if (empty($paged)) $paged = 1;
if ($pages == '') {
global $the_query;
$pages = $the_query->max_num_pages;
if (!$pages) {
$pages = 1;
}
}
if (1 != $pages) {
echo "<nav aria-label=\"...\">
<ul class=\"pagination\">";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link(1) . "\">První</a>
</li>";
if ($paged > 1 && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link($paged - 1) . "\">Předchozí</a>
</li>";
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
echo ($paged == $i) ? "<li class=\"page-item active\"><a class=\"page-link\" href=\"" . get_pagenum_link($i) . "\">" . $i . "</a></li>" : "<li class=\"page-item\"><a class=\"page-link\" href=\"" . get_pagenum_link($i) . "\">" . $i . "</a></li>";
}
}
if ($paged < $pages && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link($paged + 1) . "\">Další</a>
</li>";
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
echo "
<li class=\"page-item \">
<a class=\"page-link\" href=\"" . get_pagenum_link($pages) . "\">Poslední</a>
</li>";
echo "</ul>";
echo "</div>";
//echo "<div class='all_pages text-muted'><small>Stránek " . $pages . "</small></div>";
}
}
I hope it helps, it works for me.
(sorry for formating, stack overflow editor is totall bull...)

Categories