Search code examples
expressionengine

Expression Engine: How do I inlude a the correct category url title in the link to an article?


I am working on an EE 1.7.1 site and need to display the two most recent article entries on the home page. The challenge is that I also need to link those entries back to their respective category pages to display properly.

Here is the code I have so far:

{exp:weblog:entries weblog="articles" limit="2"}
   <p><a href="{site_url}article/{url_title}">{title}</a></p>
{/exp:weblog:entries}

Here is what I would like to do so that it brings in the corresponding :

{exp:weblog:entries weblog="articles" limit="2"}
  {categories}
   <p><a href="{site_url}{category_url_title}/{url_title}">{title}</a></p>
  {/categories}
{/exp:weblog:entries}

Unfortunately this doesn't work correctly. What is the right way to do this?

Thanks.


Solution

  • ExpressionEngine doesn't natively have a method of including the Category Name as a URL Segment when outputting the Permalink URI, unlike, say, WordPress.

    It can be done of course, but will require hacking around the standard EE URL Segments.

    If you're even the least bit curious, this was discussed in more detail on the EE Podcast, Episide #10 Behind the Scenes at Playgrounder.

    The relevant discussion between Dan Benjamin and Ryan Ireland occurs between 18:45 - 26:20, when Dan details how he developed the unique permalink structure for Playgrounder:

    Spoiler Alert: Playgrounder URLs are built using a last segment variable to lookup the Entry URL Title, while the rest of the preceding URL is the output by looping thru the entry's categories.

    For example, given the following URL:

    http://playgrounder.com/stuff/learning/books/sneaky-uses-for-everyday-things

    Would translate into EE parlance as:

    /index.php/category_url_title/.../url_title