<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>United by Design &#187; WordPress Tutorials</title>
	<atom:link href="https://www.unitedbydesign.co.za/category/wordpress-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.unitedbydesign.co.za</link>
	<description>Web &#38; Graphic Design Cape Town</description>
	<lastBuildDate>Thu, 07 Sep 2017 17:57:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Next and Previous Links with Loop on Single Pages in WordPress</title>
		<link>https://www.unitedbydesign.co.za/wordpress-tutorials/test/</link>
		<comments>https://www.unitedbydesign.co.za/wordpress-tutorials/test/#comments</comments>
		<pubDate>Fri, 20 Sep 2013 12:47:46 +0000</pubDate>
		<dc:creator><![CDATA[Renier]]></dc:creator>
				<category><![CDATA[Wordpress Tutorials]]></category>

		<guid isPermaLink="false">http://www.unitedbydesign.co.za/?p=137</guid>
		<description><![CDATA[<p>If you are looking for a relatively easy way to create a looping navigation between single posts in the same category in WordPress, then this might help. This works well for websites that have portfolios or single items that are related, providing a convenient &#8220;next&#8221; and &#8220;previous&#8221; link for the visitor. To start off check [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.unitedbydesign.co.za/wordpress-tutorials/test/">Next and Previous Links with Loop on Single Pages in WordPress</a> appeared first on <a rel="nofollow" href="https://www.unitedbydesign.co.za">United by Design</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>If you are looking for a relatively easy way to create a looping navigation between single posts in the same category in WordPress, then this might help. This works well for websites that have portfolios or single items that are related, providing a convenient &#8220;next&#8221; and &#8220;previous&#8221; link for the visitor.</p>
<p><span id="more-137"></span></p>
<p>To start off check out the <a href="http://codex.wordpress.org/Function_Reference/get_adjacent_post" target="_blank">get_adjacent_post()</a> function on WordPress Codex. This basically returns the values or information of the post adjacent to the current viewed post on the single page. The code below is an example of a link to the &#8216;next&#8217; post.</p><pre class="crayon-plain-tag">&lt;?php
    $next = get_adjacent_post(true, '', false);
	$permalinknext = get_permalink( $next-&gt;ID );
	if(!empty($next)) {
	echo '&lt;a class="nextpost" href="'.$permalinknext.'" title="'.$next-&gt;post_title.'"&gt;&lt;/a&gt;';
	}
?&gt;</pre><p>The values from the &#8216;next&#8217; post are stored as an array in the variable $next. The permalink is extracted for that specific post from $next by using the <a href="http://codex.wordpress.org/Function_Reference/get_permalink" target="_blank">get_permalink()</a> function, and stored as $permalinknext for easier use in the link. To create the link use a simple php &#8216;if (!empty())&#8217; statement to check if the $next variable isn&#8217;t empty, and if it isn&#8217;t empty; echo the link for the &#8216;next&#8217; post with a title and class for styling.</p>
<p>For the &#8216;previous&#8217; link the code stays much the same except the parameters of the <a href="http://codex.wordpress.org/Function_Reference/get_adjacent_post" target="_blank">get_adjacent_post()</a> need to be changed and variables named correctly.</p><pre class="crayon-plain-tag">&lt;?php
    $previous = get_adjacent_post(true, '', true);
	$permalinkprevious = get_permalink( $previous-&gt;ID );
	if(!empty($previous)) {
	echo '&lt;a class="prevpost" href="'.$permalinkprevious.'" title="'.$previous-&gt;post_title.'"&gt;&lt;/a&gt;';
	}
?&gt;</pre><p>While this is really useful, the &#8216;next&#8217; and &#8216;previous&#8217; links obviously disappear when you reach the end of the category, but some of you may want the navigation to loop through the whole category again and return to the start. For this you will have to retrieve both the permalinks for the first and last post to use in the if statement for when the variables $next, and $previous are empty.</p><pre class="crayon-plain-tag">&lt;?php
	if(empty($next)) {
	global $post;
	$args = array( 'numberposts' =&gt; 1, 'category' =&gt; 4, 'order' =&gt; 'ASC' );
	$myposts = get_posts( $args );
	foreach( $myposts as $post ) :
	setup_postdata($post); ?&gt;
	&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;&lt;/a&gt;
    &lt;?php
    endforeach;
    wp_reset_postdata();
    }
?&gt;</pre><p>Change <strong>&#8216;category&#8217; =&gt; 4</strong> to the category you are using. You can see what ID is assigned to a category by hovering over it in the WordPress backend under the categories tab and checking the link url.</p>
<p>By putting everything together you can create links for when the category reaches the end. The code bellow is a full example of &#8216;next&#8217; and &#8216;previous&#8217; navigation on a single page with looping functionality.</p><pre class="crayon-plain-tag">&lt;div id="navigation-next"&gt;
&lt;?php
    $next = get_adjacent_post(true, '', false);
	$permalinknext = get_permalink( $next-&gt;ID );
	if(!empty($next)) {
	echo '&lt;a class="prevpost" href="'.$permalinknext.'" title="'.$next-&gt;post_title.'"&gt;&amp;nbsp&lt;/a&gt;';
	}
?&gt;
&lt;?php
	if(empty($next)) {
	global $post;
	$args = array( 'numberposts' =&gt; 1, 'category' =&gt; 4, 'order' =&gt; 'ASC' );
	$myposts = get_posts( $args );
	foreach( $myposts as $post ) :
	setup_postdata($post); ?&gt;
	&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;&lt;/a&gt;

	&lt;?php
    endforeach;
	wp_reset_postdata();
	}

?&gt;
&lt;/div&gt;

&lt;div id="navigation-previous"&gt;
&lt;?php
    $previous = get_adjacent_post(true, '', true);
	$permalinkprevious = get_permalink( $previous-&gt;ID );
	if(!empty($previous)) {
	echo '&lt;a class="prevpost" href="'.$permalinkp.'" title="'.$previous-&gt;post_title.'"&gt;&amp;nbsp&lt;/a&gt;';
	}
?&gt;
&lt;?php
	if(empty($previous)) {
	global $post;
	$args = array( 'numberposts' =&gt; 1, 'offset'=&gt; 0, 'category' =&gt; 4 );
	$myposts = get_posts( $args );
	foreach( $myposts as $post ) :
	setup_postdata($post); ?&gt;
	&lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;&lt;/a&gt;

	&lt;?php
    endforeach;
	wp_reset_postdata();
	}

?&gt;
&lt;/div&gt;</pre><p></p>
<p>The post <a rel="nofollow" href="https://www.unitedbydesign.co.za/wordpress-tutorials/test/">Next and Previous Links with Loop on Single Pages in WordPress</a> appeared first on <a rel="nofollow" href="https://www.unitedbydesign.co.za">United by Design</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.unitedbydesign.co.za/wordpress-tutorials/test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
