My pagination doesnt work in wordpress custom theme - php

I am using pagination_bar from function.php. I implemented that in custom page called page-blog.php. I restriced pages to only 2 per page and i have 3 blog posts. After I put pagination_bar() function it shows me fatal error.
I saw some soluton already here but none of them helps. I have tried doing some offset.
function.php
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'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '<i class="fa fa-angle-left"></i>',
'next_text' => '<i class="fa fa-angle-right"></i>'
));
}
}
page-blog.php
<div class="col-xl-8 col-md-12 col-lg-8 col-sm-12 col-xs-12 md-blog-posts">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$homePagePosts = new WP_Query(array(
'posts_per_page' => '2',
'post_type' => 'post',
'has_archive' => true,
'post_status' => 'publish',
'order' => 'DESC',
'paged' => $paged,
));
if($homePagePosts->have_posts()) :
while($homePagePosts->have_posts()) :
$homePagePosts->the_post(); ?>
<div class="col-xs-12 col-md-12 col-lg-12 md-blog-blog">
<div class="md-blog-img">
<div class="img-container">
<div class="positioning">
<span class="md-blogdate-number"><?php the_time('F d, Y.'); ?></span>
<h4 class="md-blog-title"><?php echo get_the_title() ?></h4>
Read More
</div>
<div class="md-blog-img"><?php the_post_thumbnail() ?></div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<h1>There is no posts at this moment</h1>
<?php endif; ?>
<div class="md-pagination-holder" style="background: #333; width:50px; height: 50px; color: #fff;">
<?php paginate_bar(); ?>
</div>
</div>
Also you can see the website here - > http://prodenvermovers.wpupkeep.org/blog/
I get fatal error.
EDIT : http://prntscr.com/oij1dr
I chaned paginate_bar to pagination_bar as function is actually called but i get error above when i refresh the page.

Try this to see if you can get the page to work. In this example it just uses the built in WP hooks to generate pagination links. Also, by "still not working" do you mean you are still getting the error, or just not seeing the pagination?
<div class="col-xl-8 col-md-12 col-lg-8 col-sm-12 col-xs-12 md-blog-posts">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$homePagePosts = new WP_Query(array(
'posts_per_page' => '2',
'post_type' => 'post',
'has_archive' => true,
'post_status' => 'publish',
'order' => 'DESC',
'paged' => $paged,
));
if($homePagePosts->have_posts()) :
while($homePagePosts->have_posts()) :
$homePagePosts->the_post(); ?>
<div class="col-xs-12 col-md-12 col-lg-12 md-blog-blog">
<div class="md-blog-img">
<div class="img-container">
<div class="positioning">
<span class="md-blogdate-number"><?php the_time('F d, Y.'); ?></span>
<h4 class="md-blog-title"><?php echo get_the_title() ?></h4>
Read More
</div>
<div class="md-blog-img"><?php the_post_thumbnail() ?></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="nav-previous alignleft"><?php previous_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php next_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<h1>There is no posts at this moment</h1>
<?php endif; ?>
</div>

Related

Pagination wordpress does not display

I have coded my theme from base and I have a problem displaying pagination in my page.
I have a foreach over my posts that is working properly and it is showing 9 posts in every page, but I could not display paginations below that to switch between page.
It would be great if someone can help me with it.
<div class="row">
<?php
$args = array(
'posts_per_page' => 9,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post); ?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?= get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach (get_the_category($post->ID) as $category) {
echo '' . $category->name . '';
} ?> |
<span>
مدت زمان مطالعه
<?= get_field('duration'); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php endforeach; ?>
<?= previous_posts_link()?>
</div>
It does not display with any of these built in functions
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
The functions you mention:
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
All require that the global $wp_query variable contains the necessary fields to generate the pagination (namely found_posts, paged, and posts_per_page).
Since you're using get_posts, the fields in $wp_query will never be set.
Instead, you can overwrite the $wp_query variable with a WP_Query instance which means the fields will be set. It is good practice to then restore $wp_query after your loop in case you have any data later in the page or in the footer that relies on it.
Example:
global $wp_query;
$args = array(
'posts_per_page' => 9,
// 'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
// this one is important, it lets the query object know what page you're on.
'paged' => get_query_var( 'paged' ),
// are you sure you want to suppress filters ? make sure you know what this does.
'suppress_filters' => true,
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?php echo get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach ( get_the_category( get_the_ID() ) as $category ) {
echo '' . $category->name . '';
}
?>
|
<span>
مدت زمان مطالعه
<?php echo get_field( 'duration' ); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php
endwhile;
the_posts_pagination();
// restore the original $wp_query.
wp_reset_postdata();

Pagination in Wordpress home.php

Pagination isn't working in a Wordpress blog. I've tracked the specific file responsible, to home.php.
The original blog page displays posts fine (10 at a time), however as mentioned pagination isn't working.
<?php
/**
Category Page
*/
get_header(); ?>
<div class="container-fluid blogs post-section" id="blogs">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-9 col-lg-8">
<?php echo category_description( $category_id ); ?>
<div class="category-posts">
<?php
$args = array(
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => "10",
"orderby" => "date",
"order" => "DESC"
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-12">
<div class="inner-post">
<?php if ( has_post_thumbnail() ) { ?><div class="thumbnail col-sm-4" style="background:none!important;"><?php the_post_thumbnail(); ?></div><?php } ?>
<div class="abso custom_abso col-sm-8"><div class="inner-box">
<div class="date">
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('M');
$archive_day = get_the_time('d');
?>
<span class="month"><?php echo $archive_month; ?></span> <span class="day"><?php echo $archive_day; ?>,</span><span class="year"><?php echo $archive_year; ?></span>
</div>
<h2 style="margin-top:0;"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<p><a href="<?php the_permalink(); ?>" class="readmores">Read More <i class="fa fa-caret-right" aria-hidden="true"></i>
</a></p>
</div>
<?php endwhile; endif;
?></div>
<div class="pagination text-center">
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
</div>
<?php wp_reset_query(); ?>
</div>
<?php get_sidebar(); ?>
</div></div></div>
<?php get_footer(); ?>
Based on this Wordpress documentation, I've updated the top of the file, to look like the following:
<div class="category-posts">
<?php $args = array(
'posts_per_page' => 10,
'offset' => 0,
'cat' => '',
'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,
'fields' => '',
);
$posts_array = get_posts( $args ); ?>
<div class="col-sm-12">
This has worked, in that the pagination buttons work, however, only one post is displayed per page:
Any advice on how to display 10 posts per page?
Please add the following parameter 'paged'. It sets pagination query variable. The pagination is works as per this parameter.
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
to the $args.

WP Query - Pagination Buttons not showing

Pagination buttons are not showing for some reason - any help would be greatly appreciated. Also, there is 8 posts showing unlike 10 as stated in the code.
<?php get_header(); ?>
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div class="container-fluid p-0 pagewrap pagehead position-relative d-table" id="recent-projects">
<div class="container pageintro">
<div class="row">
<div class="col-12">
<h1>Recent Projects</h1>
<p>View our recent project case studies below</p>
</div>
</div>
</div>
</div>
<div class="container-fluid pt-5 pb-2">
<div class="container">
<div class="row">
<?php $catquery = new WP_Query( 'post_type=>projects&posts_per_page=10'.'&paged='.$paged );?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<div class="col-md-6">
<div class="grid">
<figure class="effect-oscar">
<?php the_post_thumbnail('large', array('style' => 'width: 100%; height: 200px; margin: 0px auto 15px auto;')) ?>
<figcaption>
<div class="m-auto">
<h2><span class="post-date"><?php echo get_the_date(); ?></span><?php the_title(); ?></h2>
<p><span><?php the_excerpt(); ?></span></p>
Read Post
</figcaption>
</figure>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<div class="pagination-nav text-center m-auto d-block p-4 mx-auto my-4">
<?php echo paginate_links( array(
'prev_text' => '<span>Previous Page</span>',
'next_text' => '<span>Next Page</span>'
)); ?>
</div>
</div>
<?php get_footer(); ?>
It is a custom post type page also
First of all your WP_Query args are incorrectly set. It should be:
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = [
'post_type' => 'projects',
'posts_per_page' => 10,
'paged' => $paged
];
$catquery = new WP_Query( $args );
?>
This is the preferred way to do it by most developers. However if you wonder what is your mistake... You set the post type with arrow notation but you must use the equal sign only.: post_type=projects
Next your pagination doesn't know about your custom post type. You must explicitly set it:
<?php
// Need a big base number:
$big = 999999999;
// Pagination
paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $catquery->max_num_pages,
'prev_text' => '<span>Previous Page</span>',
'next_text' => '<span>Next Page</span>'
) );

WP my pagination function don't show in my custom Template

My pagination function shows in my index page , but when call function in a custom Template page (page-news.php) it don't show !!
functions.php
function numbering_pagination() {
global $wp_query;
$all_pages = $wp_query->max_num_pages;
$current_page = max(1,get_query_var('paged'));
if ($all_pages >1) {
return paginate_links(array(
'base' => get_pagenum_link() . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'mid_size' => 3,
'end_size' => 3,
'prev_text' => 'السابق',
'next_text' =>'التالي'
));
}
}
page-news.php
<?php /* Template Name: news */
get_header(); ?>
<div id="fh5co-blog-section" class="fh5co-section-gray">
<div class="container">
<div>
<div class="text-center heading-section animate-box">
<h3>news</h3>
</div>
</div>
</div>
<div class="container">
<div class="row row-bottom-padded-md">
<?php
$args = array(
'post_type' => 'post',
'category_name'=> 'news'
);
$posts = new WP_Query( $args );
while( $posts->have_posts() ):
$posts->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6">
<div class="fh5co-blog animate-box">
<img src="<?php the_post_thumbnail_url('full')?>" alt="" />
<div class="blog-text">
<div class="prod-title">
<h3><?php the_title()?></h3>
<span class="posted_by"><?php the_time('F jS, Y'); ?></span>
<p><?php the_content('<span class="read-more"> ... more</span>'); ?></p>
</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>
</div>
<!-- ------ pagination ------ -->
<div class="pagination-numbers text-center">
<?php echo numbering_pagination() ?>
</div>
<!-- ------ END News ------ -->
How can I solve this?
From the WordPress codex:
<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 5,
'category_name' => 'gallery',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->
AND:
<?php
$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' => $the_query->max_num_pages
) );
?>
Mind the:
'total' => $the_query->max_num_pages
Also see: https://codex.wordpress.org/Function_Reference/paginate_links

Pagination in custom post type in wordpress

I am try to put pagination in my custom post in wordpress. my custom post type name is videos. it appears the pagination but when I click on the pagination page it goes to 404 page.
<?php
$videos= new WP_Query(array(
'post_type'=>'videos',
'posts_per_page' => 9,
));?>
<?php if($videos->have_posts()) : ?>
<?php while($videos->have_posts()) : $videos->the_post(); ?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div class="video">
<?php the_post_thumbnail(); ?>
<div class="watch">
<i class="fa fa-play"></i>
</div>
</div>
<div class="video-exerpt">
<?php the_title(); ?>
</div>
</div>
<?php endwhile; ?>
<div class="col-xs-12 text-center">
<?php
$GLOBALS['wp_query'] = $videos;
the_posts_pagination(
array(
'mid_size' => '2',
'prev_text' => '<i class="fa fa-hand-o-left"></i> Previous',
'next_text' => 'Next <i class="fa fa-hand-o-right"></i>',
'screen_reader_text' => ' '
)
);
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found', 'Bangladesh Parjatan'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
its shows the pagination bt the links are not working. Please help me.
pass your wp_query arguments like this. You should use paged argument for pagination.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$videos= new WP_Query(array(
'post_type'=>'videos',
'posts_per_page' => 9,
'paged' => $paged,
));
hopfully your pagination will work fine.
Can you please replace below code?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$videos= new WP_Query(array(
'post_type'=>'videos',
'posts_per_page' => 9,
'paged' => $paged,
)); ?>
<?php if($videos->have_posts()) : ?>
<?php while($videos->have_posts()) : $videos->the_post(); ?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div class="video">
<?php the_post_thumbnail(); ?>
<div class="watch">
<i class="fa fa-play"></i>
</div>
</div>
<div class="video-exerpt">
<?php the_title(); ?>
</div>
</div>
<?php endwhile; ?>
<?php
$total_pages = $videos->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 »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', 'Bangladesh Parjatan'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Try the following code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$videos= new WP_Query(array(
'post_type'=>'videos',
'posts_per_page' => 9,
'paged' => $paged,
)); ?>
<?php if($videos->have_posts()) : ?>
<?php while($videos->have_posts()) : $videos->the_post(); ?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div class="video">
<?php the_post_thumbnail(); ?>
<div class="watch">
<i class="fa fa-play"></i>
</div>
</div>
<div class="video-exerpt">
<?php the_title(); ?>
</div>
<?php endwhile; ?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', 'Bangladesh Parjatan'); ?></h3>
<?php endif; ?>
</div>
<nav>
<ul class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
</ul>
</nav>
<?php wp_reset_postdata();?>

Categories