Last active
January 29, 2020 11:57
-
-
Save clivewalker/653102b11dbef0720d217faebdd5aec5 to your computer and use it in GitHub Desktop.
How to add Next and Previous links to a blog post with Perch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// get the post data, put it into $post variable | |
$post = perch_blog_custom(array( | |
'filter' => 'postSlug', | |
'match' => 'eq', | |
'value' => perch_get('s'), | |
'skip-template' => true, | |
'return-html' => true, | |
'template' => 'blog/post.html', | |
)); | |
// get the post date and put into variable | |
$date = $post[0]['postDateTime']; | |
// Generate the Previous Link | |
// This is the post_prev.html template | |
// <div class="blog-nav"><p>Previous: | |
// <a href="/news/amp/<perch:blog id="postSlug" />" class="prev"><perch:blog id="postTitle" /></a></p> | |
$prev = perch_blog_custom(array( | |
'count'=>1, | |
'filter'=>'postDateTime', | |
'match'=>'lt', | |
'sort'=>'postDateTime', | |
'sort-order'=>'DESC', | |
'value'=>$date2, | |
'template'=>'blog/post_prev.html' | |
), true); // stores Prev link in a variable to use later | |
// Generate the Next Link | |
// This is the post_next.html template | |
// <p>Next: <a href="/news/amp/<perch:blog id="postSlug" />" class="next"><perch:blog id="postTitle" /></a></p> | |
// </div> | |
$next = perch_blog_custom(array( | |
'count'=>1, | |
'filter'=>'postDateTime', | |
'match'=>'gt', | |
'sort'=>'postDateTime', | |
'sort-order'=>'ASC', | |
'value'=>$date2, | |
'template'=>'blog/post_next.html' | |
), true); // stores Next link in a variable to use later | |
echo $html; // the post | |
// Previous and next post navigation | |
if (empty($prev)){ | |
echo '<div class="blog-nav">'; | |
} else { | |
echo $prev; | |
} | |
if (empty($next)){ | |
echo '</div>'; | |
} else { | |
echo $next; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment