pagination - Linking images in WordPress Paginated Post

admin2025-06-04  3

I have a paginated post on WordPress with about 5 pages. I want to set it up so that the images in the content from the previous page automatically link to the following page.

I've used the following code in functions.php file:

<?php 
  add_action('the_content',function($content) {
    global $page, $numpages, $multipage;
    if ( $multipage ) {
      $nextPage = $page + 1;
      if ( $nextPage <= $numpages ) {
        $link = _wp_link_page( $nextPage );
        $content = preg_replace('/(<img(.+?)\/>)/i','<a href="'.$link.'">$1</a>', $content);
      }
    }
    // send back our content, modified or not
    return $content;
  });
?>

The code above ALMOST works. When I substitute the $link variable for an actual URL e.g. , all the images in a paginated post end up linking to google. However, when I place the variable $link there, none of the images link anywhere. Not sure if there's an issue with me using the _wp_link_page variable.

I'm totally lost as to why it won't work when the $link variable is placed, but it works with any other value.

Hopefully someone can assist. Let me know!

Thanks.

I have a paginated post on WordPress with about 5 pages. I want to set it up so that the images in the content from the previous page automatically link to the following page.

I've used the following code in functions.php file:

<?php 
  add_action('the_content',function($content) {
    global $page, $numpages, $multipage;
    if ( $multipage ) {
      $nextPage = $page + 1;
      if ( $nextPage <= $numpages ) {
        $link = _wp_link_page( $nextPage );
        $content = preg_replace('/(<img(.+?)\/>)/i','<a href="'.$link.'">$1</a>', $content);
      }
    }
    // send back our content, modified or not
    return $content;
  });
?>

The code above ALMOST works. When I substitute the $link variable for an actual URL e.g. http://google, all the images in a paginated post end up linking to google. However, when I place the variable $link there, none of the images link anywhere. Not sure if there's an issue with me using the _wp_link_page variable.

I'm totally lost as to why it won't work when the $link variable is placed, but it works with any other value.

Hopefully someone can assist. Let me know!

Thanks.

Share Improve this question edited Jan 26, 2019 at 6:27 Kashif Rafique 2351 gold badge3 silver badges12 bronze badges asked Feb 26, 2018 at 4:31 johndoejohndoe 133 bronze badges 1
  • 1 did you try to use add_filter()? – Michael Commented Feb 26, 2018 at 4:51
Add a comment  | 

1 Answer 1

Reset to default 0

_wp_link_page() returns a HTML string and not just the URL address of the link. So, if the link's URL address is http://example/blah/2/, then _wp_link_page() would return:

<a href="http://example/blah/2/">

..i.e. it returns the opening a tag for that link.

So replace the following:

$content = preg_replace('/(<img(.+?)\/>)/i','<a href="'.$link.'">$1</a>', $content);

..with this:

$content = preg_replace('/(<img(.+?)\/>)/i', $link . '$1</a>', $content);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748973528a315290.html

最新回复(0)