How do I create next and previous buttons that will allow visitors on my website to move to the next or previous post after they are done reading the current one.
Some time ago I needed a solution for this and KK was so kind to help me out with the following code:
- Code: Select all
<cms:php>
global $CTX;
$page_ids = explode( ",", "<cms:pages ids_only='1' />" );
$cur_page_id = "<cms:show k_page_id />";
$pos = array_search( $cur_page_id, $page_ids );
if( $pos!==FALSE ){
if( $pos>0 ){
// Prev page id
$prev_page_id = $page_ids[$pos-1];
$CTX->set( 'prev_page_id', $prev_page_id, 'global' );
}
if( $pos<count($page_ids)-1 ){
// Next page id
$next_page_id = $page_ids[$pos+1];
$CTX->set( 'next_page_id', $next_page_id, 'global' );
}
}
</cms:php>
<cms:if prev_page_id >
<cms:pages id=prev_page_id skip_custom_fields='1'>
<a href="<cms:show k_page_link />"><i class="fa fa-angle-left"></i> <cms:get_custom_field 'text_for_previous_article' masterpage='globals.php' /></a>
</cms:pages>
</cms:if>
<cms:if next_page_id >
<cms:pages id=next_page_id skip_custom_fields='1'>
<a href="<cms:show k_page_link />"><cms:get_custom_field 'text_for_next_article' masterpage='globals.php' /> <i class="fa fa-angle-right"></i></a>
</cms:pages>
</cms:if>