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.
Related
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
I used the pagination function
function sjb_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="sjb-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";
}
And i am calling this function into my shortcode function.
function job_listing {
$paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
$args = array(
'posts_per_page' => $a['posts'],
'post_type' => 'jobpost',
'jobpost_category' => $a['category'],
'jobpost_job_type' => $a['type'],
'jobpost_location' => $a['location'],
'paged' => $paged,
);
$search_result = query_posts($args);
get_simple_job_board_template('job-filters.php', array('per_page' => $a['posts'], 'order' => $a['order'], 'categories' => $a['category'], 'job_types' => $a['type'], 'atts' => $atts, 'location' => $a['location'], 'keywords' => $a['keywords']));
if (have_posts()):
get_simple_job_board_template('job-listings-start.php');
while (have_posts()): the_post();
get_simple_job_board_template_part('content', 'job-listing');
endwhile;
get_simple_job_board_template('job-listings-end.php');
else:
get_simple_job_board_template('content-no-jobs-found.php');
endif;
**sjb_pagination();**
$html = ob_get_clean();
wp_reset_query();
return $html;
}
This pagination function is used in shortcode calling function.Now the problem is that pagination working correctly only one page i have created.It is not working for static Home page and other pages.Some where it is giving 404 error or somewhere it renders to home page on all navigation Why is that happening ? Please guide me i have to add this pagination feature in my plugin .If it show this kind of behaviour so it is not good for plugin users.
Thanks
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));
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(); ?>
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 ); ?>