title: How to add next and previous links in custom WordPress loop with pagination
body: <p>I created a custom loop in a Wordpress page with pagination (Reference: <a href="https://www.wpblog.com/use-wp_query-to-create-pagination/" rel="nofollow noreferrer">https://www.wpblog.com/use-wp_query-to-create-pagination/</a>)</p>
  
  <p>This is the code in "CustomPage.php":</p>
  
  <pre><code>&lt;?php
  /**
  * Template Name: Custom Page
  */
  get_header(); ?&gt;
  
  &lt;?php
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $args = array(
  'posts_per_page' =&gt; 4,
  'paged' =&gt; $paged
  );
  $custom_query = new WP_Query( $args );
  ?&gt;
  
  
  &lt;?php
  while($custom_query-&gt;have_posts()) :
  $custom_query-&gt;the_post();
  ?&gt;
  
  &lt;!-- Content Loop --&gt;
  
  &lt;?php endwhile; ?&gt;
  &lt;?php if (function_exists("pagination")) {
  pagination($custom_query-&gt;max_num_pages);
  } ?&gt;
  
  
  
  &lt;?php if (function_exists("pagination")) {
  pagination($custom_query-&gt;max_num_pages);
  } ?&gt;
  
  
  
  &lt;?php get_footer(); ?&gt;
  </code></pre>
  
  <p>This is the code for paging in "function.php" :</p>
  
  <pre><code>function pagination($pages = '', $range = 4)
  {
  $showitems = ($range * 2)+1;
  
  global $paged;
  if(empty($paged)) $paged = 1;
  
  if($pages == '')
  {
  global $wp_query;
  $pages = $wp_query-&gt;max_num_pages;
  if(!$pages)
  {
  $pages = 1;
  }
  }
  
  if(1 != $pages)
  {
  echo "&lt;div class=\"pagination\"&gt;&lt;span&gt;Page ".$paged." of ".$pages."&lt;/span&gt;";
  if($paged &gt; 2 &amp;&amp; $paged &gt; $range+1 &amp;&amp; $showitems &lt; $pages) echo "&lt;a href='".get_pagenum_link(1)."'&gt;&amp;laquo; First&lt;/a&gt;";
  if($paged &gt; 1 &amp;&amp; $showitems &lt; $pages) echo "&lt;a href='".get_pagenum_link($paged - 1)."'&gt;&amp;lsaquo; Previous&lt;/a&gt;";
  
  for ($i=1; $i &lt;= $pages; $i++)
  {
  if (1 != $pages &amp;&amp;( !($i &gt;= $paged+$range+1 || $i &lt;= $paged-$range-1) || $pages &lt;= $showitems ))
  {
  echo ($paged == $i)? "&lt;span class=\"current\"&gt;".$i."&lt;/span&gt;":"&lt;a href='".get_pagenum_link($i)."' class=\"inactive\"&gt;".$i."&lt;/a&gt;";
  }
  }
  
  if ($paged &lt; $pages &amp;&amp; $showitems &lt; $pages) echo "&lt;a href=\"".get_pagenum_link($paged + 1)."\"&gt;Next &amp;rsaquo;&lt;/a&gt;";
  if ($paged &lt; $pages-1 &amp;&amp;  $paged+$range-1 &lt; $pages &amp;&amp; $showitems &lt; $pages) echo "&lt;a href='".get_pagenum_link($pages)."'&gt;Last &amp;raquo;&lt;/a&gt;";
  echo "&lt;/div&gt;\n";
  }
  }
  </code></pre>
  
  <p>All this works perfectly!</p>
  
  <p>Now I would like to add the "prev" and "next" links in HTML head tag, but I can't do that.</p>
  
  <p>I tried to create this function in function.php, but only the "prev" link appears, "next" does not appear.</p>
  
  <pre><code>function rel_next_prev(){
  global $paged;
  
  if ( get_previous_posts_link() ) { ?&gt;
  &lt;link rel="prev" href="&lt;?php echo get_pagenum_link( $paged - 1 ); ?&gt;" /&gt;&lt;?php
  }
  
  if ( get_next_posts_link() ) { ?&gt;
  &lt;link rel="next" href="&lt;?php echo get_pagenum_link( $paged +1 ); ?&gt;" /&gt;&lt;?php
  }
  
  }
  add_action( 'wp_head', 'rel_next_prev' );
  </code></pre>
  
  <p>Can someone help me?</p>
  
  <p>For example, if I have a page with three pages, I would like to get something like this:</p>
  
  <pre><code>Page 1:
  &lt;link rel="next" href="url-page-2″&gt;     
  
  
  Page 2:
  &lt;link rel="next" href="url-page-3"&gt;
  &lt;link rel="prev" rel="url-page-1"&gt;
  
  
  Page 3:
  &lt;link rel="prev" href="url-page-2"&gt;
  </code></pre>
  
  <p>Thank you all</p>
  
  <p>P.s. Sorry my bad English, but I'm Italian;)</p>