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.

My solution for redirecting to the most recent entry (using php)

November 08, 2011 9:24am

Subscribe [1]
  • #1 / Nov 08, 2011 9:24am

    Emily Heath

    197 posts

    You have a main section which is made up of multiple entries, but you don’t have a landing or listing page (like on a blog) which you arrive at and then choose which entry you want to look at. Instead, the user clicks on the main navigation link, e.g /reviews/ and is redirected to the most recent entry, e.g. /reviews/my-latest-great-review/ and uses a sub menu to navigate between the entries.

    I’ve put this code in the index template for the template group, named ‘reviews’.

    <?php if ("{segment_2}" == "") { ?>   
    
     {exp:channel:entries 
      channel="ch_reviews" 
      disable="{pv_disable_default}" 
      limit="1"
      status="Published"
      dynamic="no"
         }<?php header('Location:{comment_url_title_auto_path}/');exit;?> {/exp:channel:entries}   
    
    <?php } elseif ("{segment_2}" != "") { ?>      
    
     {embed="reviews/_reviews"}
    
    <?php } ?>

    The first if statement checks if there is not a second segment (where the entry URL title
    would be), in which case we run the channel entries tag, get the URL title to the most recent entry, and essentially append that to the URL so that you are then looking at the 2nd statement, which is triggered when there is a segment_2. When there is already a URL title in the 2nd segment, the template embeds the single review template _reviews (instead of redirecting you, because I’m going for cruft free URLs without the extra segment/template name before the URL title.)

    Looking at it now I’m not sure that I need the second elseif statement, it could probably be just ‘else’, so maybe you can try that, but at least it works!

    I hope that helps someone else trying to do the same thing, or me when I forget next time and come looking again (don’t you just love it when you find you already answered your own question?!). OR if you know how to do this without the need for PHP, please do share.

     

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases