ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Prev/Next Entries restricted by entry's related field value?

September 04, 2012 11:00pm

Subscribe [1]
  • #1 / Sep 04, 2012 11:00pm

    James Muspratt

    8 posts

    Hi,

    I’m building a site whose primary channel is a bunch of real estate properties. So I have a “houses” channel. I wanted to group these by location, and first tried using categories to do, but it turned out I need to save more than just a title, desciption, and single image for each location. So I decided to dedicate a whole channel to locations, create all the fields I need, and then use a relationship field in the houses channel to assign each house to a location.

    This works fine: The location template basically outputs information from that particular location and then retrieves the related houses, creating a list of links to the houses to be shown in their own template:

    {exp:channel:entries channel="locations" dynamic="on" limit="1"}
    
     {location_long_name}
     {location_description}
     {location_highlights}
     {location_image}
    
    <h2>Houses in {title}:</h2>
    <p><ul><br />
     {reverse_related_entries}<br />
      <li><a href="{url_title_path='properties/house'}">{title}</li><br />
     {/reverse_related_entries}<br />
    </ul><br />
    {/exp:channel:entries}

    And my house template boils down to this:

    {exp:channel:entries channel="houses" dynamic="on" limit="1"}    
        <h1>{title}</h1>
        
     {house_description}
     {house_image}
     
    {/exp:channel:entries}


    I want to add previous and next links to this house template, but if I use the standard {exp:channel:next_entry} and {exp:channel:prev_entry} tags in the house template, they’re going to link to entries that aren’t in the same location as the current entry.

    Is there some kind of {exp:query} that could output the prev/next links that share the same location as the current entry? I found a few plugins that seemed close but don’t quite address this particular challenge.

    Thanks!

  • #2 / Sep 10, 2012 4:48pm

    James Muspratt

    8 posts

    Ok, I found a solution for the previous-next links, though it’s not particularly elegant or efficient. Code is based on this ngenworks post.


    Lets assume the previous-next links will follow our retrieval of entry page’s entry.

    {!-- Get info out of the current URL (dynamic = "on") --}
    {exp:channel:entries channel="houses" dynamic="on" limit="1"}  
    
     {!-- get this entry's related location and save some stuff with PHP --}
     <?php 
      // save this page's entry id
      $this_entry_id = '{entry_id}'; 
      {related_entries id="location"}
       //save this related location's url-title
       $this_location_url_title = '{url_title}';
       $this_location_title = '{title}';
      {/related_entries}
     ?>
     
     {!-- output house info based on the current URL --}
     
     <h1>{title}</h1>
        
     {house_description}
     {house_image}
     
    {/exp:channel:entries}

    Then to output previous-next links that only link to houses in the same location, we can just use loop through all entries, build an array of same-location entries, figure out which of those are offset by 1 before and 1 after, and then output the corresponding links.

    {!-- loop through all entries ("dynamic = off") --}
    {exp:channel:entries channel="houses" dynamic="off" orderby="number_of_bedrooms" sort="desc"}
      <?php
      // for each house, get related location and store its url_title
      {related_entries id="location"}
         $location_url_title = '{url_title}';
        {/related_entries}
       // only store house in $houses if its location-url matches this page's location url_title (saved earlier)
    if ($location_url_title == $this_location_url) {
        // Store entries in order
        $houses['{entry_id}'] = '{url_title}'; 
       }
       // get prev/next stuff
       $current = $this_entry_id;
       $keys = array_keys($houses);
       $offset = array_search($current, $keys);
       $prev_key = @$keys[$offset - 1];
       $next_key = @$keys[$offset + 1];
       $prev = ($prev_key) ? '<a href="http://{site_url}properties/house/"> . '" class="previous"]Prev Villa</a>' : "";
       $next = ($next_key) ? '<a href="http://{site_url}properties/house/"> . '" class="next"]Next Villa</a>' : "";
      ?>
    {/exp:channel:entries}
    
    <nav id="prev-next" class="cf">
       <ul>
         <li id="prev"><?php echo $prev; ?></li>
         <li id="next"><?php echo $next; ?></li>
       </ul>
    </nav>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases