Breaking down this function - php

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.

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 Stop Page Pagnitation

I am not to familiar with wordpress design, with that being said I do know a little PHP. Im trying to edit my theme so that it will stop forming page pagination i.e. stop page forming ->page2->page3 after a certain numbers of posts have been added to the page
My Question
Scratching around my theme I believe this is the code responsible for creating the 2nd page, 3rd page and so forth.
//---------------------- Pagination ---------------
function kriesi_pagination($pages = '', $range = 4)
{
$showitems = ($range)+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 ) echo "<a class='last' href='".get_pagenum_link($paged - 1)."'>PREVIOUS</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 ) echo "<a class='last' href='".get_pagenum_link($paged + 1)."'>NEXT</a>";
//if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'></a>";
echo "</div>\n";
}
}
What I tried To do
I believe the $range variable contains the number of posts before creating a second page, however after changing value of $range nothing has happened...
Commenting out the code - all posts on page 1 displays but all posts after that disappears...
Any advice here? Am I working with the wrong code snippet?
Paged is formed before the page is rendered so the code that controls the output of page numbers is too late.
You haven't identified whether this is a custom query, cpt etc so im assuming its the standard posts list. So modify as needed if not. This needs to go into your functions file.
add_action( 'pre_get_posts', 'all_posts', 1 );
function all_posts($query){
$query->set('posts_per_page', -1); // return all posts change this to the number you want
$query->set('nopaging', true);//stop add rows...
$query->set('no_found_rows', true); // dont count the rows to populate total posts count
}

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'
);

Categories