Pagination problem on the homepage in wordpress - php

I am using the following code for the homepage pagination. The problem is that the posts are not changing whenever I try to switch between the pages but I could see the pagination changes in the permalink. Can someone please help me to solve this issue?
<?php
$ourCurrentPage = get_query_var('page') ? get_query_var('page') : 1;
$args = array(
'post_type'=> 'post',
'order' => 'DESC',
'posts_per_page' => 4,
'page' => $ourCurrentPage,
);
$my_query = new WP_Query( $args );
if($my_query->have_posts()) : while ($my_query->have_posts() ) : $my_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-content" data-aos="zoom-in" data-aos-delay="200">
-------------
-------------
</div>
</article><!-- #post-## -->
<?php endwhile;
echo paginate_links(array(
'total' => $my_query->max_num_pages
));
?>

It is 'paged' not 'page'
$args = array(
'post_type'=> 'post',
'order' => 'DESC',
'posts_per_page' => 4,
'paged' => $ourCurrentPage,
);

Related

How to create a query for displaying the lessons from the current category(course) LearnDash customization?

I try to customize the course content list forming by the LearnDash plugin. Now on the lesson page in the sidebar displays just a boring list of the lessons included in the course (the current shortcode formed it). But I need to display the list of the lessons with thumbnails and other meta information from the lessons. For this purpose, I try to use this chunk of code:
<?php
$args = array(
'cat' => 153,
'post_type' => 'sfwd-courses',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
And I always see the same answer: "No Posts Sorry". Please help to sort out this issue.
Please try below code which helps you to display lessons fo specific category.
<?php
$args = array(
'post_type' => 'sfwd-lessons',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'ld_lesson_category', //double check your taxonomy name in you db
'field' => 'id',
'terms' => 26,
),
),
);
$q = new WP_Query($args); ?>
<div class="timeline"> <?php
if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>

pagination doesn't work on a single page of WordPress

Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.

Wordpress - Pagination

i'm creating a blog page in my custom theme wordpress, i would like use pagination in my list posts, my query with list of posts work well but the pagination doesn't work. i see always the same first 2 posts.
page-blog.php
/*
* Template Name: Pagina Blog
*/
<?php get_header(); ?>
<?php
$posts_per_page = 2;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
//$posts_array = get_posts( $args );
$wp_query = new WP_Query( $args );
//query_posts( $args );
?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- Title -->
<h1 class="my-4"><?php the_title() ?></h1>
<h2 class="card-title">
<?php the_title(); ?>
</h2>
<!-- Content -->
<?php the_content() ?>
<!-- Pagination -->
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php endwhile;
// Reset Query
wp_reset_query(); ?>
<?php get_footer(); ?>
I think there some issue with the loop, but i'm not sure, i can see "Newer" and "Older" links but when i go on older i see the same posts, so "www.mysite.com/blog" have the same post of "www.mysite.com/blog/2" :(
Here lies your problem:
'offset' => 0,
Replace that with:
'offset' => $posts_per_page * ($paged - 1)
Alternativley, if I'm reading the documentation correctly, you can maybe just remove the offset value entirely.

Show posts by user

So, here is my php to show posts for wordpress:
<div class="rfp_hide" id="rhm_profile_item">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 20,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product, $post, $paged;
?>
<div class="rhm_post_container">
Posts go here
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
It shows all the posts regardless of who posted.
Each post has its href as following:
<a class="royal_author_link" href="<?php echo $userpro->permalink( $post->post_author ); ?>">
On the post author page, the permalink has following structure :
example.com/profile/someone.
Now, how can I modify it so that when I am in a specific user page, it only shows the posts by that user only?
Did you use a custom plugin to create the profile page?
In case you have access to the user ID related to the page, you can extend the query argument by the attribute "author", like so:
$args = array(
'author' => $yourUserID
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 20,
'orderby' => 'date',
'order' => 'DESC'
);

Query with wp_pagenavi?

I use the following code to query all the posts
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'san-pham', 'posts_per_page' => 8,
'paged' => $paged,
'order' => 'DESC',
)
);
?>
<?php $i=1; while(have_posts()) : the_post(); ?>
//
<?php $i++; endwhile ?>
<?php if (function_exists('wp_pagenavi')) {
wp_pagenavi(); } ?>
Now I want to query all the post from a specific category. How do I do this?
You can add the category_name in the query_posts array
For example
query_posts('showposts=3&category_name=Blog&orderby=ID&paged='.$paged);
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'san-pham',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8,
'order' => 'DESC',
);
$query = new WP_Query($args);
if ($query->have_posts()):
while ( $query->have_posts() ):
$query->the_post();
?>
//content
<?php endwhile; ?>
<?php if (function_exists('wp_pagenavi')): ?>
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
<?php endif; ?>

Categories