Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hi guys,


So far I have two templates, one called 'Author' and the other called Chapter.
In the chapter template, I have actual 'chapter' content for example an editable region called 'Chapter Title' and 'Contents' for the actual contents of that particular chapter.

The Author template simply has a summary and bio of the author.

So my webpage works by letting people choose an author. Once chosen it brings them to a cloned Author page, there Chapter pages that have been related to the author are displayed.

So, I click on Author John Doe and Click on Chapter 2.It loads up Chapter 2 and its contents for me to read. I want to have a next and previous button, I have tried a generic prev and next code but it actually cycles through EVERY chapter from even DIFFERENT authors.

I have related both templates using (this is placed in chapter.php)
Code: Select all
<cms:editable type='relation' name='project_link'  masterpage='author.php' has='one'  />


Any ideas?

I was originally using this modified snippet from cheesypoof/KK
Code: Select all
<cms:set cur_page=k_page_name 'global' />
<cms:set prev_page='' 'global' />
<cms:set next_page='' 'global' />
<cms:set found_cur_page='0' 'global' />

<cms:pages skip_custom_fields='1'>
   <cms:if found_cur_page=='0' >
      <cms:if k_page_name==cur_page >
         <cms:set found_cur_page='1' 'global' />
      <cms:else />
         <cms:set prev_page=k_page_link 'global' />
      </cms:if>
   <cms:else />
      <cms:if next_page==''>
         <cms:set next_page=k_page_link 'global' />
      </cms:if>
   </cms:if>
</cms:pages>

<cms:if prev_page>
   <a href="<cms:show prev_page />">Prev</a>
</cms:if>
<cms:if next_page>
   <a href="<cms:show next_page />">Next</a>
</cms:if>


I've tinkered a little but am not sure how to get it to display properly, it just cycles to every chapter at the moment.
Hi,

The post where you got that code from, has another solution right below it - viewtopic.php?f=8&t=7087#p9409

We can tweak that to loop through only pages belonging to a particular author (the author of the chapter being visited in your case).

Please try this -
Code: Select all
<cms:related_pages>
    <!-- author of the current chapter -->
    <cms:set my_current_author=k_page_name 'global' />
</cms:related_pages>

<cms:if my_current_author>
    <cms:php>
       global $CTX;
       $page_ids = explode( ",", "<cms:pages custom_field="project_link=<cms:show my_current_author />" 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>


<cms:if prev_page_id >
   <cms:pages id=prev_page_id skip_custom_fields='1'>
      <a href="<cms:show k_page_link />">Prev</a>
   </cms:pages>
</cms:if>
<cms:if next_page_id >
   <cms:pages id=next_page_id skip_custom_fields='1'>
      &nbsp;<a href="<cms:show k_page_link />">Next</a>
   </cms:pages>
</cms:if>

The code above is to be placed within the 'page-view' of your chapter.php.

To explain briefly the code - we first find out the author of the current chapter
Code: Select all
<cms:related_pages>
    <!-- author of the current chapter -->
    <cms:set my_current_author=k_page_name 'global' />
</cms:related_pages>

And then find the IDs of all the chapters related to that particular author (remember 'project_link' is the name of your relation field) -
Code: Select all
<cms:pages custom_field="project_link=<cms:show my_current_author />" ids_only='1' />

Hope it helps.
As usual KK, your solutions are always on spot.

Although I won't implement it straight away, I will use this as a reference to learn more about couchcms and web developing in general! Thank you and I will be supporting you guys with a purchase of a couchCMS license soon!

And hopefully in the not too distant future, I'll be able to help provide solutions to the other members as well :)
3 posts Page 1 of 1