Jump to content

Next/previous links excluding pages based on options field

Recommended Posts

Hi all,

I have the following simple code for next/previous links on article pages:

<!-- Next article -->
<?if($page->next->id) { ?><i class="icon-chevron-left"></i> <a href="<?echo $page->next->url; ?>"> <?=${'previous_'.$countryCode}; ?></a><? } ?>
<!-- /Next article -->

<!-- Previous article -->
<?if($page->prev->id) { ?> <a href="<?echo $page->prev->url; ?>"><?=${'next_'.$countryCode}; ?></a> <i class="icon-chevron-right"></i> <? } ?>
<!-- /Previous article -->

It all works correctly but there's a certain type of article I need to exclude from the next/previous links (any page that has options field 'content_type' with a value of '6').

I'm trying to figure out a way to get 'next' and 'previous' but if 'content_type' on that page has a value of '6' then skip it.

Can anyone help with some clues on how this might be done? I've had a look at using the pagination code as well as looking at grabbing 'siblings' and filtering them but come to a dead end.


Share this post

Link to post
Share on other sites
$list = $page->siblings("content_type!=6");


Wonderfully simple solution - thank you LostKobrakai!

For anyone interested this was my final code - all working correctly:

$list = $page->siblings("content_type!=6,sort=article_publish_date");

$prevPage = $list->getPrev($page);
$nextPage = $list->getNext($page);

if($prevPage) echo "<i class='icon-chevron-left'></i> <a href='{$prevPage->url}'>" . ${'previous_'.$countryCode} . "</a>";
if($nextPage) echo "<a href='{$nextPage->url}'>" . ${'next_'.$countryCode} . "</a> <i class='icon-chevron-right'></i>";

  • Like 1

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By John W.
      A little guide to generating an sitemap.xml using (I believe) a script Ryan originally wrote with the addition of being able to optionally exclude child pages from being output in the sitemap.xml file.
      I was looking back on a small project today where I was using a php script to generate an xml file, I believe the original was written by Ryan. Anyway, I needed a quick fix for the script to allow me to optionally exclude children of pages from being included in the sitemap.xml output.
      A good example of this is a site where if you visit /minutes/ a page displays a list of board meetings which includes a title,  date, description and link to download the .pdf file.
      I have a template called minutes and a template called minutes-document. The first page, minutes, when loaded via /minutes/ simply grabs all of its child pages and outputs the name, description and actual path of an uploaded .pdf file for a visitor to download.
      In my back-end I have the template MINUTES and MINUTES-DOCUMENT. Thus:

      So, basically, their employee can login, hover over minutes, click new, then create a new (child) record and name it the date of the meeting e.g. June 3rd, 2016 :

      Outputting the sitemap.xml and optionally excluding children that belong to a template.
      The setup of the original script is as follows:
      1. Save the file to the templates folder as sitemap.xml.php
      2. Create a template called sitemap-xml and use the sitemap.xml.php file.
      3. Create a page called sitemap.xml using the sitemap-xml template
      Now, with that done you will need to make only a couple of slight modifications that will allow the script to exclude children of a template from output to the sitemap.xml
      1. Create a new checkbox field and name it:   sitemap_exclude_children
      2. Add the field to a template that you want to control whether the children are included/excluded from the sitemap. In my example I added it to my "minutes" template.
      3. Next, go to a page that uses a template with the field you added above. In my case, "MINUTES"
      4. Enable the checkbox to exclude children, leave it unchecked to include children.
      For example, in my MINUTES page I enabled the checkbox and now when /sitemap.xml is loaded the children for the MINUTES do not appear in the file.

      A SIMPLE CONDITIONAL TO CHECK THE "sitemap_exclude_children" VALUE
      This was a pretty easy modification to an existing script, adding only one line. I just figure there may be others out there using this script with the same needs.
      I simply inserted the if condition as the first line in the function:
      function renderSitemapChildren(Page $page) { if($page->sitemap_exclude_children) return ""; ... ... ...  
      <?php /** * ProcessWire Template to power a sitemap.xml * * 1. Copy this file to /site/templates/sitemap-xml.php * 2. Add the new template from the admin. * Under the "URLs" section, set it to NOT use trailing slashes. * 3. Create a new page at the root level, use your sitemap-xml template * and name the page "sitemap.xml". * * Note: hidden pages (and their children) are excluded from the sitemap. * If you have hidden pages that you want to be included, you can do so * by specifying the ID or path to them in an array sent to the * renderSiteMapXML() method at the bottom of this file. For instance: * * echo renderSiteMapXML(array('/hidden/page/', '/another/hidden/page/')); * * patch to prevent pages from including children in the sitemap when a field is checked / johnwarrenllc.com * 1. create a checkbox field named sitemap_exclude_children * 2. add the field to the parent template(s) you plan to use * 3. when a new page is create with this template, checking the field will prevent its children from being included in the sitemap.xml output */ function renderSitemapPage(Page $page) { return "\n<url>" . "\n\t<loc>" . $page->httpUrl . "</loc>" . "\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" . "\n</url>"; } function renderSitemapChildren(Page $page) { if($page->sitemap_exclude_children) return ""; /* Aded to exclude CHILDREN if field is checked */ $out = ''; $newParents = new PageArray(); $children = $page->children; foreach($children as $child) { $out .= renderSitemapPage($child); if($child->numChildren) $newParents->add($child); else wire('pages')->uncache($child); } foreach($newParents as $newParent) { $out .= renderSitemapChildren($newParent); wire('pages')->uncache($newParent); } return $out; } function renderSitemapXML(array $paths = array()) { $out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; array_unshift($paths, '/'); // prepend homepage foreach($paths as $path) { $page = wire('pages')->get($path); if(!$page->id) continue; $out .= renderSitemapPage($page); if($page->numChildren) { $out .= renderSitemapChildren($page); } } $out .= "\n</urlset>"; return $out; } header("Content-Type: text/xml"); echo renderSitemapXML(); // Example: echo renderSitemapXML(array('/hidden/page/'));  
      In conclusion, I have used a couple different processwire sitemap generating modules. But for my needs, the above script is fast and easy to setup/modify.
      - Thanks
    • By a-ok
      Hi folks,
      I'm wondering if it is possible to create Previous/Next links between pages that only have a certain checkbox ticked? I have a Projects > each project structure setup, and if the user checks for a project to be a case study, it generates some new/different fields in the backend. What I'm after is to be able to have Previous/Next links per project that only cycles through the case studies... not all the projects.
      Here below is my code that works for ALL projects but not just CASE STUDY projects.
      Thanks for your time,
      <?php /* $page->project_casestudy_check === 1 */ ?> <div class="project-casestudy-pagination-container clearfix">   <div class="container-fluid">    <div class="row">     <div class="col-xs-12 col-sm-12">      <?php if ($page->prev->id) : ?>      <div class="prev-project-container">       <a class="prev-project" href="<?php echo $page->prev->url; ?>"><span>Previous Project</span></a>      </div>      <?php else : ?>      <div class="prev-project-container">       <a class="prev-project" href="<?php echo $page->siblings->last->url; ?>"><span>Previous Project</span></a>      </div>      <?php endif; ?>      <?php if ($page->next->id) : ?>      <div class="next-project-container">       <a class="next-project" href="<?php echo $page->next->url; ?>"><span>Next Project</span></a>      </div>      <?php else : ?>      <div class="next-project-container">       <a class="next-project" href="<?php echo $page->siblings->first->url; ?>"><span>Next Project</span></a>      </div>      <?php endif; ?>     </div>    </div>   </div>
    • By a-ok
      Hi folks,
      I'm set up a simple next/prev between pages that are loaded in via pjax with the following:
      <?php if ($page->next->id) : ?> <div class="next-solution-container">      <a data-pjax class="next-solution" href="<?php echo $page->next->url; ?>"><span><?php echo $page->next->title; ?></span></a> </div> <?php endif; ?> This worked great, but I want it now to loop... so if it's the last item, it gets the first item as the next link. I thought the following below would work, but as I am using it on the page template, and thus using $page, I don't think first/last are in use?
      <?php if ($page == $page->last) : ?> <div class="next-solution-container">      <a data-pjax class="next-solution" href="<?php echo $page->first->url; ?>"><span><?php echo $page->first->title; ?></span></a>     </div> <?php else : ?> <div class="next-solution-container">      <a data-pjax class="next-solution" href="<?php echo $page->next->url; ?>"><span><?php echo $page->next->title; ?></span></a>     </div> <?php endif; ?> Any thoughts?
    • By a-ok
      I am setting up some simple pagination for a page, and want to use the 
      $page->next  or 
      $page->prev functions.
      Everything works as I want, except for one part – checking whether a next/prev page exists or not.
      So, for example, this works within my code
      <?php if ($page->next->journal_featured_image) : ?> but if I wrapped the whole section in 
      <?php if ($page->next) : ?> it doesn't seem to make any difference. I tried count too... but no use. Any ideas?
    • By Mike Rockett
      I'm new to PW, and think it's fantastic! Never, ever, have I seen such an amazing CMF!
      Anyways, to my question: I had set some Hanna Code to render a list of page suggestions on the http404 page, which works perfectly. The problem, however, comes in when I use repeaters, which are page instances.
      The page suggestion renderer has the following:
          $suggestions = $pages->find('name|title|summary|body%='.$request);
      I have a page that is named services-industries, and repeaters with the word services in the name. As such, repeaters appear in the suggestion results-list if applicable to the search.
      How can I exclude/remove those?
  • Create New...