WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

By default, wp_link_pages (the code which allows you to breakup posts into multiple pages) creates spaces between each number. Is there a way to remove these spaces via functions.php?

share|improve this question
up vote 1 down vote accepted

Just replace a> <a with a><a:

echo str_replace( 'a> <a', 'a><a', wp_link_pages( array ( 'echo' => FALSE ) ) );

If you want to remove the spaces around unlinked numbers too, I suggest a separate function in your theme’s functions.php to keep the code readable:

function trimmed_link_pages( $args = array () )
    $args['echo'] = FALSE;
    $links = wp_link_pages( $args );
    $links = str_replace(
        array ( 'a> ', ' <a', ':<a' ),
        array ( 'a>',  '<a',  ': <a' ),
    print $links;

Use it like wp_link_pages():

if ( have_posts() )
    while ( have_posts() )
        print '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';

Related: Changing Link Attributes for Wp_Link_Pages.

share|improve this answer
That doesn't seem to be working for me, are you simply adding this to functions.php? – AndrettiMilas Jan 5 '13 at 19:28
I am using this instead of the naked wp_link_pages() in a theme. The function does not provide an output filter unfortunately. – toscho Jan 5 '13 at 19:46
Out of curiosity, any idea how to remove the spaces around the current page also... since it is not linked? – AndrettiMilas Jan 5 '13 at 20:40
Thanks, but I actually can't get that new function to work for whatever reason. With my limited PHP knowledge, I assume it should work. How would you implement it? – AndrettiMilas Jan 5 '13 at 21:03
@siouxfan45 See my update. Tested, works. – toscho Jan 5 '13 at 21:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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