WordPress: forcefully generate pagination url for 1st page - php

I'm trying to generate pagination URL for first page. I used the following code.
get_pagenum_link(1); // gives http://www.website.com/
anyhow,
get_pagenum_link(2); // gives http://www.website.com/paged/2/
get_pagenum_link checks for the page # if($paged == 1) {// Don't include Page# in URL } I want to disable this check so that, it always include page #in URL
Also, http://www.website.com/paged/1/ is redirected to http://www.website.com/ I added a filter
add_filter('redirect_canonical','pif_disable_redirect_canonical');
function pif_disable_redirect_canonical($redirect_url) {
return false;
}
but, I think, its not a right way to do, it'll stop all canonical redirections.

Add this function into theme’s functions.php
function custom_pagination() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
Usage :
To call this function you have to add the below php code in your theme’s index.php, archive.php, category.php, or any other page template.
<?php custom_pagination(); ?>

Related

Add transitional pages to pagination

I need to add pagination to transient pages now when a have 9 pages it looks like this:
1 2 3 ... 9
I would like to give a middle point between 3 and 9, i.e.
1 2 3 ... 6 ... 9
I also want the same middle subpage to be when I'm on the last / middle page. But this should be done by yourself knowing how to take the first step.
All my ideas fail. Does anyone have an idea for such a solution?
Full working code here:
<?php
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li class="prev_post">%s</li>' . "\n", get_previous_posts_link('<') );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li >…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li class="next_post">%s</li>' . "\n", get_next_posts_link('>') );
echo '</ul></div>' . "\n";
A general pagination codeflow might look like the following:
<?php
function getPageDisplayArray(int $currentPage, int $totalCount, int $pageLen = 10): array
{
$paginationArr[$currentPage] = 1;
$totalPagesToDisplay = floor(($totalCount) / $pageLen);
//Get preceding pages logic
$pagesBefore = $currentPage - 1;
$iBefore = 2; //Number of pages you want befpre the current page
while($pagesBefore > 0 && $iBefore > 0) {
$paginationArr[$pagesBefore--] = 0;
$iBefore--;
}
//Get anteceding pages
$pagesAfter = $totalPagesToDisplay - ($pagesBefore + 1);
$iAfter = min([$pagesAfter, 2]); //Number of pages you want after your current page
while(-$iAfter < 0) {
//Either return the two pages after or don't if there are no pages after
$paginationArr[$iAfter + $currentPage] = 0;
$iAfter--;
}
if($pagesAfter > 2) {
//Calcluate Midpoint
$midpoint = floor($totalPagesToDisplay / 2) + 1;
if(!isset($paginationArr[$midpoint])) {
$paginationArr[$midpoint] = 0;
}
}
//Link the first page always if it hasn't already been set
if(!isset($paginationArr[1])) {
$paginationArr[1] = 0;
}
//Link the final page if it has not already been set.
if(!isset($paginationArr[$totalPagesToDisplay])) {
$paginationArr[$totalPagesToDisplay] = 0;
}
ksort($paginationArr);
return $paginationArr;
}
Where the Current Page, page length, and Total count are your parameters. The here will show two pages on either side as Display pages, as well as the first, last, and middle pages of your set for any arbitrart set. This will return a config array which sets the "Active" page with a value of one, and all of the others with a key of their page number and a value of zero. The rest would be a matter of displaying the values in your layout as you see fit.

Show previous/next only when there is a previous/next page [duplicate]

This question already has answers here:
Wordpress: Check if there are previous posts before displaying link
(4 answers)
Closed 5 years ago.
I am trying to get a pagination on the index.php working. What I come up with yet looks something like this:
<nav>
Previous Page
Next Page
</nav>
However, I'd like to show the Previous/Next buttons only, when there actually exists a previous/next page. How can I achieve that?
Thanks!
Put this where you want to display previous/next page
<?php wpbeginner_numeric_posts_nav(); ?>
and in functions.php add
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link('<i class="fa fa-angle-left"></i>') );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link('<i class="fa fa-angle-right"></i>') );
echo '</ul></div>' . "\n";
}
Source: WPBegginer

Is there anyway to add rel="next"/rel="prev" into "Page Template" with WordPress SEO by Yoast?

Our site is currently using "WordPress SEO by Yoast"
rel="next" and rel="prev" is working fine on category and archive page, however in page template that we created, rel="next" and rel="prev" is not showing.
(This page template also has pagination)
Our website structure => we have "Article" Post type
in Article we have category
Credit card
Cash card
Loan
etc.
As I want the url to be www.sitename.com/loan without having ../category/loan
I created 'Page' called 'Loan' and using page-loan.php as page template to query Post type 'Article' category 'Loan'
I want to have rel="next" and rel="prev" appear in this page template as well
I wonder is there anyway to use WordPress SEO by Yoast to do it?
or is there anyway to modify below script in the plugin to make rel="next" and rel="prev" appear in Page template as well?
the script I found in the plugin
public function adjacent_rel_links() {
// Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
/**
* Filter 'wpseo_genesis_force_adjacent_rel_home' - Allows devs to allow echoing rel="next" / rel="prev" by WP SEO on Genesis installs
*
* #api bool $unsigned Whether or not to rel=next / rel=prev
*/
if ( is_home() && function_exists( 'genesis' ) && apply_filters( 'wpseo_genesis_force_adjacent_rel_home', false ) === false ) {
return;
}
global $wp_query;
if ( ! is_singular() ) {
$url = $this->canonical( false, true, true );
if ( is_string( $url ) && $url !== '' ) {
$paged = get_query_var( 'paged' );
if ( 0 == $paged ) {
$paged = 1;
}
if ( $paged == 2 ) {
$this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
}
// Make sure to use index.php when needed, done after paged == 2 check so the prev links to homepage will not have index.php erroneously.
if ( is_front_page() ) {
$url = wpseo_xml_sitemaps_base_url( '' );
}
if ( $paged > 2 ) {
$this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
}
if ( $paged < $wp_query->max_num_pages ) {
$this->adjacent_rel_link( 'next', $url, ( $paged + 1 ), true );
}
}
}
else {
$numpages = 0;
if ( isset( $wp_query->post->post_content ) ) {
$numpages = ( substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1 );
}
if ( $numpages > 1 ) {
$page = get_query_var( 'page' );
if ( ! $page ) {
$page = 1;
}
$url = get_permalink( $wp_query->post->ID );
// If the current page is the frontpage, pagination should use /base/
if ( $this->is_home_static_page() ) {
$usebase = true;
}
else {
$usebase = false;
}
if ( $page > 1 ) {
$this->adjacent_rel_link( 'prev', $url, ( $page - 1 ), $usebase, 'single_paged' );
}
if ( $page < $numpages ) {
$this->adjacent_rel_link( 'next', $url, ( $page + 1 ), $usebase, 'single_paged' );
}
}
}
}
/**
* Get adjacent pages link for archives
*
* #since 1.0.2
*
* #param string $rel Link relationship, prev or next.
* #param string $url the un-paginated URL of the current archive.
* #param string $page the page number to add on to $url for the $link tag.
* #param boolean $incl_pagination_base whether or not to include /page/ or not.
*
* #return void
*/
private function adjacent_rel_link( $rel, $url, $page, $incl_pagination_base ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
if ( $page > 1 ) {
$url = add_query_arg( 'paged', $page, $url );
}
}
else {
if ( $page > 1 ) {
$base = '';
if ( $incl_pagination_base ) {
$base = trailingslashit( $wp_rewrite->pagination_base );
}
$url = user_trailingslashit( trailingslashit( $url ) . $base . $page );
}
}
/**
* Filter: 'wpseo_' . $rel . '_rel_link' - Allow changing link rel output by WP SEO
*
* #api string $unsigned The full `<link` element.
*/
$link = apply_filters( 'wpseo_' . $rel . '_rel_link', '<link rel="' . esc_attr( $rel ) . '" href="' . esc_url( $url ) . "\" />\n" );
if ( is_string( $link ) && $link !== '' ) {
echo $link;
}
}
First of all, you have to make a backup of your database. You can achieve this on phpmyadmin or with some plugin that do the backup for you.
Next, you should do the url thing. You can remove the category path from categories in the page "Search Appearance - Yoast SEO", click on tab Taxonomies, then remove the Category URLs.
On the pagination issue, you could manually input the links on each page or use a php function to get all the links of post type articles like this:
wp_list_pages(array(
'child_of' => $post->post_parent,
'exclude' => $post->ID));

Easy Digital Downloads and Pagination

I'm trying to get my pagination working with a custom downloads page on my site. I'm using Easy Digital Downloads as my store backend and I'm calling the store items in a custom wp_query. I have the same code working to paginate my archives, but for some reason, it's not working here. I would greatly appreciate any help that can be offered. Here is my code:
From my functions.php file
/* Pagination numbers function
------------------------------------
Allows Google-like page numbers to post pages by calling function */
function custom_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
MY Query
<?php
// Allow Pagination
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
// Arguments
$type = 'download';
$args=array(
'post_type' => $type,
'posts_per_page' => -1,
'posts_per_page' => 16,
'paged' => $paged
);
// Get results
$my_download_query = new WP_Query($args);
// The Loop
if( $my_download_query->have_posts() ) {
while ($my_download_query->have_posts()) : $my_download_query->the_post(); ?>
<div id="store-item">
<?php
// Check for thumbnail
if ( has_post_thumbnail() ) {
echo '<a href="' . the_permalink() . '" title="' . the_title_attribute() . '" >';
echo the_post_thumbnail('store-thumb');
echo '</a>';
}?>
<?php
$content = get_the_content();
echo string_limit_words($content,20) . '...';
?>
<!-- Get Price -->
<p class="price"><?php echo edd_price_range( $download_id ); ?></p>
<div class="call-to-action-button">
<a type="button" href="<?php the_permalink()?>">Details</a>
</div>
<?php edit_post_link(); ?>
</div> <!-- END #store-item -->
<?php endwhile; ?>
<!-- Call custom numbered pages -->
<?php custom_numeric_posts_nav(); ?>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
<?php } ?> <!-- endif -->
?>
That's because inside custom_numeric_posts_nav() you're using the global variable $wp_query, whereas $my_download_query in your custom query.
You could change custom_numeric_posts_nav() to accept the variable $my_download_query as an optional parameter and, if passed, use that:
function custom_numeric_posts_nav( $query = '' ) {
if(!$query)
$query = $GLOBALS['wp_query'];
/** Stop execution if there's only 1 page */
if( $query->max_num_pages <= 1 )
return;
// ... and change the rest of the function accordingly ...
}
And this when you call it:
<!-- Call custom numbered pages -->
<?php custom_numeric_posts_nav( $my_download_query ); ?>

Pagination Doesn't work with wordpress custom post

Pagination Doesn't work with word press custom post.
i have tried these code below,but pagination doesn't appear.
tried different query but no result.
i am new in php and word press.i just copy and past code.
can anyone please help me?
what i have done so far are below.
in function.php
/*pagination*/
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
in custom post query
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<div class="page_news">
<div class="single_page_news">
<h2><?php the_title(); ?><h2>
<p><?php the_content(); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
please help me
Use These code
$paged = ( get_query_var('page') ) ? get_query_var('page') :1;
$query = new WP_Query( array( 'posts_per_page' => 1,'paged' => $paged,'post_type' => 'achievements','orderby' => 'date', 'order' => 'ASC' ) );
while ( $query->have_posts() ) : $query->the_post();
endwhile;
Instead of
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
Follow this link
http://thenetapp.com/2014/01/how-to-list-wordpress-posts-with-pagination/
Your pagination function is only set up for the default main query, not for a custom query.
Also, don't use get_posts for paginated queries. It it is nice function to use for a custom query, but it becomes a bummer to work with once you require pagination.
Rather use WP_Query for paginated queries, it is much easier to use.
Example:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'post_type' => 'YOUR POST TYPE'
);
$q = new WP_Query($args);
if($q->have_post()) {
while($q->have_posts()) {
$q->the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
wp_reset_postdata();
You can have a look at the codex for extra parameters.
You now need to change every instance of $wp_query in your pagination function to $q for it to work.
Just a point of note, you don't have to call the $post global
EDIT
From your comments, there is a much easier way to accomplish your goal without any custom query
This page is an archive page that is meant to display your custom post type latestnews. You can simply just rename your archive-custom.php to archive-latestnews.php. See the Template Hierarchy. Just make sure has_archive is set to true when you register your post type
You should also never shop the main query for a custom query on any type of archive page. It is always troublesome, as you can see. So, delete your custom query and replace it with the default query
This is all you should have in your archive page
if(have_post()) {
while(have_posts()) {
the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
Just change all the instances of $q back to $wp_query again. Everything should work then
For extra info, you have to check out this post I've done on WPSE

Categories