Adding pagination to contents - woocommerce

So, I have following code to display products in Woocommerce
<?php
get_header()
?>
<div class="rfp_hide" id="rhm_product_show">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 20,
'product_cat' => '',
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product, $userpro, $post, $paged;
?>
<div class="rhm_indi_product rhm_profile_items">Contents</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<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>
</div>
I am getting /page/2 but the content does not change and only displays the same stuffs.
Am I doing it right?
Thanks

Related

Configure pagination so that starting from the second page a different Wordpress template is used

I have a blog start page that displays several blocks of articles.
The first block is the output of the last published article
The second block is the output of articles from the 2nd to the 7th
article
The third block is the output of articles starting from the 8th
article and pagination is connected to it
If I click "go to the next page", then everything that was on the start page is displayed on it, and I need to display the feed of posts (12 posts) starting from the second page, starting from the 14th post, and on each subsequent page just continue to display all posts in turn.
Tell me, is there a solution to this problem and how best to implement it?
Post output code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'offset' => 7,
'posts_per_page' => 6,
);
query_posts($args);
?>
Pagination output code:
<?php endwhile ; ?>
<div class="list-item">
<!--paginate-->
<?php the_posts_pagination (
$args = array(
'show_all' => false,
'end_size' => 0,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'add_args' => false,
'add_fragment' => '',
'screen_reader_text' => __( 'Posts navigation' ),
'type' => 'list',
)
);
?>
</div>
<?php else : ?>
<?php endif ; ?>
Here is my answer. In this case, I created 4 cycles that display the required number of posts in different blocks of the page. The fifth cycle was also created, which starts working from the moment we switched to the second page of posts and further.
For pagination, I used the_posts_pagination as it was required in my technical specification and in order for it to work correctly I used the global variable WP_Query
<?php
if(!get_query_var('page')) {
?>
<div class="wrap-blog-home-page">
<?php
$args = array(
'posts_per_page' => 1,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$queryLasPost = new WP_Query( $args );
?>
<?php
if ( $queryLastPost->have_posts()):
while ( $queryLastPost->have_posts() ) : $queryLastPost->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
<section class="bodyContent">
<?php
$args = array(
'offset' => 1,
'posts_per_page' => 6,
);
$queryFirstBlock = new WP_Query( $args );
?>
<?php
if ( $queryFirstBlock->have_posts()):
while ( $queryFirstBlock->have_posts() ) : $queryFirstBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
<section class="bodyContent">
<?php
$args = array(
'offset' => 8,
'posts_per_page' => 6,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$querySecondBlock = new WP_Query( $args );
?>
<?php
if ( $querySecondBlock->have_posts()):
while ( $querySecondBlock->have_posts() ) : $querySecondBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
</section>
<section class="bodyContent">
<?php
$args = array(
'posts_per_page' => 12,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$queryThirdBlock = new WP_Query( $args );
?>
<?php
if ( $queryThirdBlock->have_posts()):
while ( $queryThirdBlock->have_posts() ) : $queryThirdBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
</section>
<?php
} else {
?>
<section class="bodyContent">
<?php
$args = array(
'posts_per_page' => 12,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$queryThirdBlock = new WP_Query( $args );
?>
<?php
if ( $queryThirdBlock->have_posts()):
while ( $queryThirdBlock->have_posts() ) : $queryThirdBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<!-- nextpage -->
<?php else : ?>
<?php endif ; ?>
</section>
<?php
}
?>
<section class="bodyContent">
<?php
global $wp_query;
$restore_wp_query = $wp_query;
$wp_query = $queryThirdBlock;
?>
<div class="list-item">
<!--paginate-->
<?php the_posts_pagination (
$args = array(
'show_all' => false,
'end_size' => 0,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'add_args' => false,
'add_fragment' => '',
'screen_reader_text' => __( 'Posts navigation' ),
'type' => 'list',
)
);
?>
<?php
$wp_query = $restore_wp_query;
?>
</div>
</section>
</div>
<?php get_footer(); ?>

pagination for custom post type archive page

I'm trying to add pagination to following code inside archive-CustomPostType.php page but it doesn't show pagination or it shows empty page.
My code:
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif;
?>
</div>
<?php get_footer(); ?>
I'm using page navi for pagination but I don't think it's the problem because I tried with normal wordpress pagination but didn't help.
I think the issue is coming from get_queried_object and I don't know how to handle this.
Any help would be appreciated.
You Can Try This Code.
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => 'Previous',
'next_text' => 'Next',
) );
?>
</div>
<?php else : ?>
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
?>
</div>
<?php get_footer(); ?>
Modifying query post type in functions.php
add_action( 'pre_get_posts' ,'wpse222471_query_post_type_portofolio', 1, 1 );
function wpse222471_query_post_type_portofolio( $query )
{
if ( ! is_admin() && is_post_type_archive( 'customposttype' ) && $query->is_main_query() )
{
$query->set( 'posts_per_page', 6 ); //set query arg ( key, value )
}
}

Display post for a week

Can someone help me how to display the post only for a week? i have this code that works fine in post_date:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?
Thanks.
This Might help you to get solution:
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The previous answer was along the good track. Just modify your date_query:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>

To get pagination in my wiget custom post type list

I have get the custom post type by category in widget. Now i want to paginate those post if those more than 10 posts.For that i tried with this code
<ul class="posts-list">
<?php
$cats = get_the_category();
$cat_name = $cats[0]->name;
$paged= isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
query_posts(array ( 'category_name' => $cat_name, 'posts_per_page' => 6, 'paged' => $paged));
while (have_posts()) : the_post();
// do whatever you want
?>
<li>
<h5><?php the_title(); ?></h5>
<p><strong>posted on</strong> <?php the_time(' F jS, Y') ?> <strong>by</strong> <?php the_author(); ?></p>
<p><strong>Posted in</strong> <span><?php print_r($cat_name); ?></span></p>
<?php
endwhile;
?>
</li>
<?php
$pag_args1 = array(
'format' => '?paged=%#%',
'current' => $paged,
'total' => 3,
'add_args' => array( 'paged' => $paged)
);
echo paginate_links( $pag_args1 ); ?>
</ul>
But i cant get the exact result.
you may try like the below
<ul class="posts-list">
<?php
$cats = get_the_category();
$cat_name = $cats[0]->name;
if ( get_query_var('paged') )
{ $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array ( 'category_name' => $cat_name, 'posts_per_page' => 6, 'paged' => $paged));
while (have_posts()) : the_post();
// do whatever you want
?>
<li>
<h5><?php the_title(); ?></h5>
<p><strong>posted on</strong> <?php the_time(' F jS, Y') ?> <strong>by</strong> <?php the_author(); ?></p>
<p><strong>Posted in</strong> <span><?php print_r($cat_name); ?></span></p>
<?php
endwhile;
?>
</li>
<?php
$pag_args1 = array(
'format' => '?paged=%#%',
'current' => $paged,
'total' => 3,
'add_args' => array( 'paged' => $paged)
);
echo paginate_links( $pag_args1 ); ?>
</ul>

Pagination for my custom post type in widget

I have displayed the custom post type in the widget. Now i want to add pagination in the last. because i have more than 10 posts in my custom post type.
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
Try this one and enter your custom post type's name in 'post_type' => 'your custom post type name'
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'paged' => $paged1,
'post_type' => 'your custom post type name'
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
?>
<?php endif;
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $previous_post->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
wp_reset_postdata(); ?>
</ul>

Resources