1

I am trying to return only next post page links in my wordpress website. Here is my implementation

<?php
   wp_link_pages( array(
    'next_or_number' => 'next',
    'nextpagelink' => 'next',
    'previouspagelink' => false,
    'before' => '<div class="page_links">',
    'after'  => '</div>'
  ) );
?>

Output by this piece of code at main post page (page = 1)

<div class="page_links">
  <a href="http://localhost/circorunning/?p=542&amp;page=2">next</a>
</div>

It's fine when It's on the first instance of the page but as soon as I click next , I am getting this output by html.

<div class="page_links">
   <a href="http://localhost/circorunning/?p=542"></a> 
   <a href="http://localhost/circorunning/?p=542&amp;page=3">next</a>
</div>

Now I am on second post page and you can see above piece of output, I have this link<a href="http://localhost/circorunning/?p=542"></a>I don't want this anchor tag output at all on any page. It's not displaying on the first page but when you are on second page it does display again and again till the end.

but I want only one link, there should be no link to the main post.

I want to achieve link to the next page of the post only and when there is no post page, It should display a custom link that I setup in the function.

<div class="page_links"> 
   <a href="http://localhost/circorunning/?p=542&amp;page=3">next</a>
</div>

And I do want to use this outside of the loop.

0

Here's a suggestion, using the wp_link_pages_link filter to remove the previous links:

add_filter( 'wp_link_pages_link', function( $link, $nextorprev )
{   
    return get_query_var( 'page' ) > $nextorprev ? '' : $link;

}, 10, 2 );

We can also create a plugin that removes empty previous/next links.

So if we have one of the following:

'nextpagelink' => '',
'nextpagelink' => false,
'nextpagelink' => null,

or

'previouspagelink' => '',
'previouspagelink' => false,
'previouspagelink' => null,

then these links will be removed.

Here's the skeleton for such a plugin:

<?php
/** 
 * Plugin Name: Remove Empty Next/Previous Content Pagination Links 
 * Plugin URI:  http://wordpress.stackexchange.com/a/239335/26350
 */

add_filter( 'wp_link_pages_args', function( $args )
{
    if( empty( $args['previouspagelink'] ) )
        add_filter( 'wp_link_pages_link', 'wpse_remove_prev_links', 10, 2 );

    if( empty( $args['nextpagelink'] ) )
        add_filter( 'wp_link_pages_link', 'wpse_remove_next_links', 10, 2 );

    return $args;       
} );

add_filter( 'wp_link_pages', function( $output, $args )
{
    if( empty( $args['previouspagelink'] ) )
        remove_filter( 'wp_link_pages_link', 'wpse_remove_prev_links', 10, 2 );

    if( empty( $args['nextpagelink'] ) )
        remove_filter( 'wp_link_pages_link', 'wpse_remove_next_links', 10, 2 );     

    return $output;
}, 10, 2 );

function wpse_remove_prev_links( $link, $nextorprev )
{
    return get_query_var( 'page' ) > $nextorprev ? '' : $link;
}

function wpse_remove_next_links( $link, $nextorprev )
{
    return get_query_var( 'page' ) < $nextorprev ? '' : $link;
}

This will not override the default settings, if we leave out the 'nextpagelink' or 'previouspagelink' arguments.

Here's a way to add a custom link at the last page:

add_filter( 'wp_link_pages_link', function( $link, $nextorprev ) use ( &$numpages )
{   
    return get_query_var( 'page' ) === $numpages ? 'CUSTOM LINK' : $link;

}, 11, 2 );

where we have the priority later than in our plugin.

  • I want to remove main post link as well e.g. when I am on the 2 or 3 paginated post link it should not display the anchor tag to main post. and when there is no next page then it should display a custom link that I setup in the function somehow. – Harman Preet Sep 15 '16 at 9:42
  • I'm not sure what you mean exactly by the main post link. Did you try out the plugin? Please add all relevant information into the question itself. I will update the answer regarding how to add a custom link at the last page. @HarmanPreet – birgire Sep 15 '16 at 10:18
  • Just updated the question – Harman Preet Sep 15 '16 at 11:23
  • "And I do want to use this outside of the loop." - According to the documentations wp_link_pages() must be used within the loop. My answer assumes that. It shows how to remove the emtpy links and how to add a custom link at the last page. @HarmanPreet – birgire Sep 15 '16 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.