Author latest posts pagination - php

I'm having difficulties in adding pagination for custom wordpress query showing latest author's posts from category 'blog'. I found this code somewhere on stackoverflow. Could someone help me/give some tips how to add pagination to this query? I've spent on it couple hours and couldn't came up with something working.
Best for me would be numeric pagination:
1 | 2 | 3 | 4 | so on...
functions.php
function the_latest_author_posts($post) {
$relatedargs = array(
'author' => $post->post_author,
'post__not_in' => array( $post->ID),
'posts_per_page' => 8,
'category_name' => 'blog',
);
$relatedquery = new WP_Query( $relatedargs );
while($relatedquery->have_posts()){
$relatedquery->the_post();
$ID = get_the_ID();
?>
<div class="post-blog">
<?php
if(has_post_thumbnail()) {
$relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'medium', false);
$relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'full', false);
?>
<?php } ?>
<h1><span><?php the_title(); ?></span></h1>
<h6><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo get_the_time('j') . '/' . get_the_time('m') . '/' . get_the_time('Y') . ' '; ?></h6>
<?php
$content = get_the_content();
echo wp_trim_words( $content , '25' ); ?>
</div>
<?php wp_reset_postdata();}}
I call this function in my template in this way:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_latest_author_posts($post);
}
}
?>

First add this function in your functions
function numeric_pagination($pages = '', $range = 2){
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
} }
and use it after your loop
numeric_pagination();

Related

pagination not working for taxonomy

In functions.php I have a pagination function ..
function bd_pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
which works flawlessly with my custom post type movies and standard posts.
But when I try to use the function in my taxonomy-genre-mystery.php it doesn't work.
Doesn't work : no errors, keeps displaying page 1 even if another page is selected.
Any help please
Steven
You use get_pagenum_link, a function dedicated to page not archive page.
You need to search around get_post_type_archive_link and add manually the page at the end of the url
Page <?php echo $i; ?>
Hope it helps

wordpress pagination code display only first page

I want to apply pagination to my custom post type. Here is the pagination function I have used.
function roots_pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\">";
echo "<ul><li><span>Page ".$paged." of ".$pages."</span></li>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."'>« First</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a></li>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li><a class=\"current\" href=\"#\">".$i."</a></li>":"<li>".$i."</li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<li>Next ›</li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($pages)."'>Last »</a></li>";
echo "</ul></div>\n";
}
}
and here is the code i have used to call for pagination.
$paged = (get_query_var('paged'))?get_query_var('paged'):1;
//print_r($_SESSION);
//$paged = $_SESSION['paged'];
$temp=$wp_query;
$wp_query=null;
$wp_query=new Wp_Query();
$wp_query->query(array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'news',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged'=>$paged
));
global $wp_query;
//$temp=$wp_query;
$wp_query->in_the_loop=true;
while ($wp_query->have_posts() ) : the_post();
//if($post_title !="27 Annual Convocation & Students Recognition Day"):
$title = get_the_title();
?>
<h4 style="border-top: medium double #dedcd2;">
<?php the_title();//echo $single_news->post_title; ?>
</h4>
<div class="text_exposed_root text_exposed" id="id_5485361e9d3362c15508684">
<p>
<?php echo substr(get_the_content(), 0, 250)."..."; //$single_news->post_content ?>
View Details
</p>
</div>
<?php
endwhile;
roots_pagination();
?>
when i run this code it always display first page no matter If i click on page links. It creates page numbers fine but on clicking on links it display only first page. Any help please.

WP - Pagination in category loop not working

I have problem with pagination in my loop in category.php
Link send me to: ?cat=9&paged=2 don't show posts just home page.
My loop:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=2&post_type=blog'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
//CONTENT
endwhile;
get_template_part('pagination');
$wp_query = null;
$wp_query = $temp; // Reset
and this is function:
function silon_pagination($pages = '', $range = 4){
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged))
$paged = 1;
if($pages == ''){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
$pages = 1;
}
if(1 != $pages){
echo "<div class=\"pagination\"><span>Strona ".$paged." z ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages)
echo "<a href='".get_pagenum_link(1)."'>« Pierwsza</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Poprzednia</a>";
for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i."";
}
}
if ($paged < $pages && $showitems < $pages)
echo "Następna ›";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages)
echo "<a href='".get_pagenum_link($pages)."'>Ostatnia »</a>";
echo "</div>\n";
}
}
function is from here:
http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin
Please help :)
Had a look at the tutorial, I use exactly the same pagination function in my theme. Great function. I don't know why you are using get_template_part('pagination'); Your pagination can just be simply called by silon_pagination(); in place of get_template_part('pagination');
while debugging i found out that default posts per page option should be less than any custom posts_per_page in query_posts function. That's all. Weird, but that's the fact.

Pagination not working in Wordpress

Can't figure out why this stopped working all of a sudden. The website is: http://www.revival.tv/sermons/topical-messages/
once it goes to page two it just uses the archive template :( not sure why it is doing this, it worked fine for the last year. Wondering if a Wordpress update did it, we just noticed it.
I am using the following for the pagination:
function pagination($pages = '', $range = 2) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
}
}
if($pages != 1) {
echo "<div class='pagination group'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? "<span class='current-page'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
and then this rewrite for the sermons:
// Rewrite the Sermon URL
function sermon_rewrite() {
$cpt = 'sermon';
$tax = 'series';
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&paged=$matches[2]',
'top'
);
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/([^/]+)/?',
'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$cpt.'=$matches[2]',
'top'
);
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/([^/]+)/([^/]+)/?',
'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$tax.'=$matches[2]&'.$cpt.'=$matches[3]',
'top'
);
}
add_action('init','sermon_rewrite');
// Update Wordpress with the rewrite
function sermon_link( $link, $post = 0 ) {
$cpt = 'sermon';
$tax = 'series';
$parent = null;
$terms = get_the_terms($post->ID, $tax);
if( !$terms ) {
$child = 'other';
} else {
$child_obj = array_pop($terms);
$parent_obj = get_term_by('id', $child_obj->parent, $tax);
$child = $child_obj->slug;
if ( $parent_obj ) {
$parent = $parent_obj->slug;
}
}
if ( $post->post_type == $cpt && $terms ) {
return home_url(user_trailingslashit($cpt.'s/'.$parent.'/'.$child.'/'.$post->post_name));
} else if ( $post->post_type == $cpt && !$terms ) {
return home_url(user_trailingslashit($cpt.'s/'.$child.'/'.$post->post_name));
} else {
return $link;
}
}
add_filter('post_type_link', 'sermon_link', 1, 3);
I think I have found the problem (I'm just trial and erroring here using your provided URL). The following URL successfully loads page 2.
http://www.revival.tv/sermons/topical-messages/page/2/?post_type=sermons
Which leads me to think that simply adding the s after index.php?post_type='.$cpt should do the trick. See sample below:
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
'index.php?post_type='.$cpt.'s&'.$tax.'=$matches[1]&paged=$matches[2]',
'top'
);

Breaking down this function

I am using this function for pagination in my site and I am trying to figure it out.
Why are the variables $pages and $ranger assigning values in the parameter field? Why not just set them inside the function?
Also, how is the page adding &paged=(page#) to the URL?
If someone could help me break down this function it would be greatly appreciated. I want to eventually just have a next and previous link instead off all the numbers that are generate so I want to minimzie this function.
here is the function
<?php
function kriesi_pagination($pages = '', $range = 2) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
The $pages = '', $rage = 2 in the parameter list is defining default values for those arguments, thereby making them optional. In this way, if you call kriesi_pagination() without arguments, it is the same as calling it with the values shown.
The page ID being added to the links is most likely done with the get_pagenum_link() function, which you have not shown here.

Categories