简体   繁体   中英

How to add next and previous links in custom WordPress loop with pagination

I created a custom loop in a Wordpress page with pagination (Reference: https://www.wpblog.com/use-wp_query-to-create-pagination/ )

This is the code in "CustomPage.php":

<?php
/**
* Template Name: Custom Page
*/
get_header(); ?>

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 4,
'paged' => $paged
);
$custom_query = new WP_Query( $args );
?>


<?php
while($custom_query->have_posts()) :
$custom_query->the_post();
?>

<!-- Content Loop -->

<?php endwhile; ?>
<?php if (function_exists("pagination")) {
pagination($custom_query->max_num_pages);
} ?>



<?php if (function_exists("pagination")) {
pagination($custom_query->max_num_pages);
} ?>



<?php get_footer(); ?>

This is the code for paging in "function.php" :

function 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>Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; 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 && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
echo "</div>\n";
}
}

All this works perfectly!

Now I would like to add the "prev" and "next" links in HTML head tag, but I can't do that.

I tried to create this function in function.php, but only the "prev" link appears, "next" does not appear.

function rel_next_prev(){
global $paged;

if ( get_previous_posts_link() ) { ?>
<link rel="prev" href="<?php echo get_pagenum_link( $paged - 1 ); ?>" /><?php
}

if ( get_next_posts_link() ) { ?>
<link rel="next" href="<?php echo get_pagenum_link( $paged +1 ); ?>" /><?php
}

}
add_action( 'wp_head', 'rel_next_prev' );

Can someone help me?

For example, if I have a page with three pages, I would like to get something like this:

Page 1:
<link rel="next" href="url-page-2″>     


Page 2:
<link rel="next" href="url-page-3">
<link rel="prev" rel="url-page-1">


Page 3:
<link rel="prev" href="url-page-2">

Thank you all

Ps Sorry my bad English, but I'm Italian;)

the_posts_pagination( array(
    'mid_size'  => 2,
    'prev_text' => __( 'Back', 'Previous' ),
    'next_text' => __( 'Onward', 'Next' ),
) );

this function can help you because it's already created on WP so it's really easy to use it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM