$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pagination - Linking images in WordPress Paginated Post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pagination - Linking images in WordPress Paginated Post

matteradmin9PV0评论

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);
Post a comment

comment list (0)

  1. No comments so far