Previous and next links only for the current item

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
gap_tooth_clan

Previous and next links only for the current item

Post by gap_tooth_clan »

I wanted previous and next links that only showed me pages within the current hierarchy.

Thanks to hematec who I took the majority of this code from.

http://forum.cmsmadesimple.org/index.php?topic=34180.0

Create a UDT called hierarchy

global $gCms;
$pos = $gCms->variables['position'];
$base = explode('.', $pos);
$smarty->assign('h_toplevel', $base[0] * 1);
$smarty->assign('h_parent',   $base[ (count($base) > 1) ? count($base)-2 : 0 ] * 1);
$smarty->assign('h_this',     $base[count($base)-1] * 1);
$smarty->assign('h_level',     count($base) * 1);
$smarty->assign('f_position',     $gCms->variables['friendly_position']);
$smarty->assign('f_base',     (int)$gCms->variables['friendly_position']);


and put this in your template

{hierarchy}
{assign var="NewLevel" value=$h_level+1}
{assign var='count' value=0}
{assign var="f_pos_next" value=$f_position+0.1}
{assign var="f_pos_prev" value=$f_position-0.1}
{assign var="f_base" value=$f_base}
{assign var="f_ceil" value=$f_base+1}
{if $NewLevel > 1}
  {if $f_pos_next {menu template='V2 Main Menu' start_element=$f_pos_next number_of_levels="1"}
  {/if} 
  {if $f_pos_prev >= $f_base}
  {menu template='V2 Main Menu' start_element=$f_pos_prev number_of_levels="1"}
  {/if} 
{/if}
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm

Re: Previous and next links only for the current item

Post by RonnyK »

Or use CGSimpleSmarty,

which has the parameters included, to know if a page is a sibling....

F.e. this logic, coming from the help of CGSimpleSmarty, which could be set in the template.
{$cgsimple->get_sibling("prev","prev_sibling")}{if !empty($prev_sibling)}{cms_selflink page="$prev_sibling" text="Previous"}{/if}
And the same for the next sibling
{$cgsimple->get_sibling("next","next_sibling")}{if !empty($next_sibling)}{cms_selflink page="$next_sibling" text="Next"}{/if}
Ronny
gap_tooth_clan

Re: Previous and next links only for the current item

Post by gap_tooth_clan »

Ha ha awesome, much better
steven_germany
Forum Members
Forum Members
Posts: 34
Joined: Tue Apr 21, 2009 6:52 pm

Re: Previous and next links only for the current item

Post by steven_germany »

Ronny: Perfect! Thank you very much for this tip!
Post Reply

Return to “Tips and Tricks”