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.
Related
I want to display pagination on category pages of WordPress theme
This is my code
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$posts = get_posts(array(
'numberposts' => 150,
'posts_per_page'=>16,
'offset' => 0,
'category__not_inint' => array($category),
'post_status'=>'publish',
'order'=>'ASC'
));
foreach($posts as $post){
setup_postdata($post);
$city_name = get_field( "city-name" );
$display = '' . $city_name . '';
}
the_posts_pagination();
wp_reset_query();
return $display;
and displays the pagination. But the results of all pages are similar to the first page.
You shouldn't be using get_posts if you need the query to be paginated.
Whilst it can be done, this is a total ball ache to achieve. Instead, you should be looking at WP_Query.
Further reading on WP_Query - WP_Query # wordpress.org
Your code could look something like the following;
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; // explain to wordpress we need this paged
$wp_query = new WP_Query(array( // the query
'post_type' => 'post',
'post_category' => '',
'post_status' => 'publish',
'numberposts' => 150,
'posts_per_page' => 15,
//'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged));
while ($wp_query->have_posts()) : $wp_query->the_post(); // the loop
// some code to make it look pretty
?>
<div class="post-grid">
<a href="<?php the_permalink(); ?>">
<h3 class="card-title"><?php the_title(); ?> </h3>
</a>
</div>
<?php endwhile;
echo ( paginate_links($args = array(
'base' => site_url().'%_%', // site_url prefix is needed for pagination on homepage
'format' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $paged,
'show_all' => false,
'end_size' => 2,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => 'Prev',
'next_text' => 'Next',
'type' => 'list',
'add_args' => false,
'add_fragment' => ''
)));
wp_reset_query();
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,
);
Hi everyone so I got this piece of code to display posts but instead or rendering posts it renders the about us page can anyone help. I'm running WordPress 4.5.3
<?php get_header(); ?>
<?php
if(have_posts() ):
while(have_posts() ) : the_post();
the_title();
the_content();
endwhile;
endif;
?>
<?php get_footer(); ?>
Use This :
$args = array(
'posts_per_page' => 10,
'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
);
query_posts($args);
if(have_posts()): the_post();
while(have_posts()): the_post();
?>
<h4> <?php the_title();?> </h4>
<?php endwhile;?>
<?php endif; ?>
I’m trying to create many WordPres post and page. I include the post and page to various category. In each category I add post and page both. In this circumstances I need to show post and page under the particular category. And I want to sort the post and page ascending or descending under the Category. I need the PHP Coding this purpose. Please Give me assistance. I have created category.php by the code bellow.
<div class="cate-top ">
<h1 class="cat-page-title"><?php printf( __( ' Your are Browsing: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>
<?php if ( category_description() ) : // Show an optional category description ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
<?php while(have_posts()): the_post();?>
</div>
<div class="category-page">
<div class="cate-inn ">
<h2> <?php the_title();?></h2>
<div class="cat-image fix">
<?php the_post_thumbnail();?>
</div>
<div class="cat-read-more fix">
<?php read_more(0);?>Read More
</div>
</div>
<?php endwhile;?>
You can use get_posts or WP_Query to get the page and post with your desired category, For example
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
You can just change your category name in args,
If you are using custom taxonomy instead default category, you may use following code
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'';
endwhile;
}
}
For more help you can VISIT, for get_post you may VISIT
I've got this code which simply displays all the posts for a particular author:
<?php
$all_active_tasks = get_posts(array(
'numberposts' => -1,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><?php the_title(); ?></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
But what I can't figure out is how to paginate the results to say 10 per page. I've looked at the official codex but nothing I tried seemed to work.
Any help is appreciated.
You can use paged together with posts_per_page parameters. WP_Query. You can grab current page like this:
$paged = get_query_var( 'paged' ) ?: ( get_query_var( 'page' ) ?: 1 );
and then use it in your query:
$all_active_tasks = get_posts(array(
'posts_per_page' => 10,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post',
'paged' => $paged
));
In this way if you put /page/2/ at the end of your url, the query will return the posts from 11 to 20.
How to create the pagination itself, you can check these articles:
here and here.
Setting 'numberposts' => -1 means to get all records. You have to set for the first 10 records (0-9 records)
'numberposts' => 10,
'offset' => 0,
And for next 10, (10-19 records)
'numberposts' => 10,
'offset' => 10,
And for next 10, (20-29 records)
'numberposts' => 10,
'offset' => 20,
Try this working code
put this function in functions.php file of your active theme
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' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('<i class="fa fa-angle-double-left"></i>'),
'next_text' => __('<i class="fa fa-angle-double-right"></i>'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<div class='col-md-12'><nav class='custom-pagination pagination'>";
echo $paginate_links;
echo "</nav></div>";
}
}
and here is your modified code to work for pagination
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$all_active_tasks = get_posts(array(
'posts_per_page' => -1,
'paged' => $paged,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><?php the_title(); ?></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php if (function_exists(custom_pagination)) {
custom_pagination(count($all_active_tasks),"",$paged);
}?>
<?php wp_reset_postdata(); ?>