Woocommerce custom products list pagination - php

I have code like this for fetching and displaying products in woocommerce:
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 30
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
}
The problem is while the code displays 30 products at a time as specified, it does not add page links so other products could be viewed, is there a way to do this or am I missing somethin?

<ul class="products">
<?php
global $paged;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_postdata(); ?>
</ul><!--/.products-->
Reference : Pagination in WooCommerce

Can you please check below code? i hope this code is work for you.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'product',
'posts_per_page' => 30,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
endif;
wp_reset_postdata();

Related

Displaying post by category from query with pagination

Can you tell me what is the problem with my code? my code is to display the custom post by category. my code is perfectly working fine. but when I put a pagination, the code is error now.
<?php
$ourteam_category_check = '4';
$paged = (int) get_query_var('paged'); --> I inserted this
$niche_ourteam_args = array(
'posts_per_page' => 10,
'paged' => $paged, --> I inserted this
'post_type' => 'shop',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
),
'tax_query' => array(
array(
'taxonomy' => 'shop_cat',
'field' => 'term_id',
'terms' => $ourteam_category_check
),
),
);
$niche_ourteam = new WP_Query($niche_ourteam_args);
while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
?>
--> echo all the items from category 4 here
<?php endwhile; endif; ?>
<div class="page-nav-area">
<?php
if( function_exists('wp_pagenavi') ) {
wp_pagenavi(array('query' => $the_query));
}
?>
</div>
Try this code is working fine for my side
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="the_loop">
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'shop',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID, // your categories or you can use 'category__in' => array(2,6)
),
),
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop code goes here.
<?php endwhile; ?>
<?php if ($loop->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $loop;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>

Wordpress - Pagination doesn't work with custom loop but working in blog pages

I have a loop with custom post types, and pagination doesn't appear, when I enter the URL with /page/2, /page/3... it shows the content correctly, but links don't appear on the page.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parent_only_query = new WP_Query(array(
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
//content
}
pagination(); ?>
Archive page with pagination working:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?>
<?php endif; ?>
<?php pagination(); ?>
add this in your functions.php
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
<?php }
}
display on page.php
<?php pagination_nav(); ?>
pagination can be shown in custom post type archive template and on custom template as well.
Pagination for archive template.
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
new WP_Query( $args );
// Call pagination function before wp_reset_postdata()
the_posts_pagination( array(
'prev_text' => '<span class="fa fa-angle-left" aria-hidden="true"></span>',
'next_text' => '<span class="fa fa-angle-right" aria-hidden="true"></span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
'mid_size' => 3,
) );
Pagination for custom Template
// Get current page.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array(
post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
$query = new WP_Query( $args );
$totalPage=$query->max_num_pages;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $totalPage
) );
You can check WordPress official document on Wordpress Codex

WordPress pagination repeating first page

This use to work, until wordpress updated and now the pagination on a custom page is repeating the first page. Page 2 for example will just show up page 1 content.
I've been looking and looking and nothing is helping.
I'm calling wordpress functions outside of wordpress install with
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
then the wordpress loop is (and it's probably outdated and faulty, but this was the only one that worked at the time)
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 20,
//'showposts' => 20,
'orderby'=> 'menu_order',
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
)
)
);
query_posts( $args); ?>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/producttemplate.php";
include_once($path);
?>
</div>
<br class="clear" />
<div class="wrapper">
<div class="pagination">
<p><br /><?php pagination_bar($args); if(!$_GET['viewall']){ ?>
<br />
<a class="all" href="<?php echo add_query_arg( array( 'view' => 'all' ), get_pagenum_link(1) ); ?>">Show All</a><br /></p>
<?php } }?>
<?php wp_reset_query(); ?>
and then my custom pagination function is
//Pagenation
function pagination_bar() {
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
echo paginate_links(array(
'base' => #add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $total_pages,
));
}
}
Thank you
The following are two lines copied out of your second code dump:
$current_page = max(1, get_query_var('paged'));
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
Notice the query_var is paged?
Now, have a look at this copy from the first line of the first dump:
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
Notice the difference? page, instead of paged.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
also add this code to function.php, you need to just replace url (client-testimonials) with your page url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
After pulling whats left of my hair out, I solved it. It was this part
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
Needs to be
require $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
I'm currently using both as a fallback. I don't know if it needs to just be one. If someone has more info if it's safe to use both?

Nest a while loop in an if else statement in WordPress

I have a while loop that gets all pages of a certain category on index.php which is my posts page.
However, I want to change the code below, so if it is the homepage, it does not run this loop but displays some text.
This code the loop works:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
?>
In this code, nothing loads on any page except the homepage:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC' ,
'paged'=>$paged
);
if ( is_home() ) {
echo 'Welcome!';
} else if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
You need to wrap your query and loop in your is_home() condition
if ( !is_home() ) { // This is not the home page
// Add your query and loop here
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
} else { // This is the homepage
echo 'Some text here';
}
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $the_query->the_post();
if ( is_home() ) {
echo 'Welcome!';
}
else{?>
<h2><a href="<?php echo get_permalink(); ?>"><?php the_title();
}
<?php endwhile; endif; ?>

WordPress Taxonomy, listing ONLY the single taxonomies

So I have a custom Taxonomy 'Sectors'.
I am trying to display all the single posts in 'Sectors' in a list, just a basic list.
I currently do have it so, it will display the category, then a sub category, then the list from there.
I also have this, which lists all posts - (NOT TAXONOMY) :
<?php
$uncat = get_cat_ID('uncategorised');
$args = array(
'posts_per_page' => 5,
'category__not_in' => array($uncat)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h3><?php the_title(); ?></h3>
<span class="entry-date-orange"><strong><?php echo get_the_date(); ?></strong></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
is there a way, to use this code below, to ONLY get items from the taxonomy 'sectors'?
For this to work, you will need to get all the terms belonging to your taxonomy, and then use a tax_query to query the correct posts. For effeciency, we will only get the term ids and not the complete objects
(NOTE: The code is untested and requires PHP 5.4+. Also, just confirm the taxonomy name)
$term_ids = get_terms( 'SectorCategories', ['fields' => 'ids'] );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = [
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => [
[
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
]
],
// REST OF YOUR ARGUMENTS
];
$loop = new WP_Query( $args );
EDIT
For older PHP versions, use the following code
$term_ids = get_terms( 'SectorCategories', array( 'fields' => 'ids' ) );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = array(
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
)
),
// REST OF YOUR ARGUMENTS
);
$loop = new WP_Query( $args );
EDIT 2
A very basic loop for the above query would look something like this
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
the_title();
}
next_posts_link( 'Next posts', $loop->max_num_pages );
previous_posts_link( 'Previous posts' );
wp_reset_postdata();
}

Categories