2

This is quite a basic question I think, but when viewing a template with entries with the standard entries tag:

{exp:channel:entries channel="mywebsite"}

and if I replace the last segment with arbitrary letters, it will show all entries from that channel, like:

www.somewebsite.com/mytemplategroup/sdjhkkajsd

In a page with regular entries I can avoid this by using entry_id=segment_2, but this doesn't work when viewing a category with category titles enabled:

http://www.somewebsite.com/mytemplategroup/category_template/category/category_url_title

Replacing category_url_title with "lzdsfuhkhjfdg" shows ALL entries from the channel. I can't prevent this in the entries tag with

 {exp:channel:entries channel="mywebsite" category_url_title="segment_4"}

Because that is not supported. So how would you solve this?

Thank you very much!

6

On a single entry page (ie one with a url_title or entry_id in the URL) you can use the require_entry="yes" parameter, by using this in conjunction with the {if no_results} conditional, to only show a "proper" page when a valid entry is passed.

{exp:channel:entries require_entry="yes"}
  {if no_results}
    {redirect="site/404"}
  {/if}
  <h2>{title}</h2>
  ...
{/exp:channel:entries}

For category pages things are a little more complex as you note. I tend to make use of Low's Seg2Cat extension, which provides additional variables in your template of the format segment_n_category_id, and perhaps most handily {last_segment_category_id} so you can then do:

 {if last_segment_category_id == ''}
   {redirect="site/404"}
 {/if}

You can then just leave your channel:entries tag with the (default) dynamnic="yes" and it will pull the relevant entries for the cat from the URL, but redirect to your 404 if there isn't a valid category in the URL.

All that said, rather than using a separate template for category listings I frequently just use the same template for my "list" view as for my category view, eg I have URLs like example.com/group/template (list) and example.com/group/template/category/category_url_title (category)

I then use Seg2Cat to conditionally output the category name/info in my template if present:

{if last_segment_category_id}
   <h1>{last_segment_category_name} News</h1>
   {last_segment_category_description}
{if:else}
   <h1>Latest News</h1>
{/if}
{exp:channel:entries limit="10" channel="news"}
  ...
  entry content in here
  ...
{/exp:channel:entries}

That way you have one fewer templates to manage, and you don't have to worry so much about having valid categories in your URL

2
  • Looks like a perfect solution, I've already implemented the entry pages, and that works fine. For categories, I'll have to try that out later, but apparently this is standard behavior for EE sites with categories? Strange...
    – noregt
    Dec 8 '12 at 10:23
  • Just to round this off, I've used the seg2cat from Low wich had the advantage of not having to use the category indicator in the url. I could have squashed it all in one template/segment, but there's to much chance that the entry url title will be the same as category url title, so I keep this apart. Thanks Tom!
    – noregt
    Dec 21 '12 at 14:16
0

I'm with @Tom Davies on this one. Require entry, but you can also do so by using

url_title="{segment_3}"

But make sure you set-up the 'no_results' option, either to redirect or just a statement on page. But that stops the prevention of random urls. Some bots/spiders do this as well, and you'll see a search engine has tons of erroneous links indexed.

2
  • Thanks, I had something similar like "entry_id=segment_2", but withour the no_results. require_entry="yes" seems a bit more flexible, because that is segment independent.
    – noregt
    Dec 8 '12 at 10:26
  • require_entry is more flexible. We actually had a situation where we didn't want that to be true. So we used the url_title="{url_title}". The issue for us, was we wanted ONLY the designated post to display, not any post. Dec 8 '12 at 16:54
0

Good answers above, but as an easy first line of defense I also always include a limit in my channel:entries tag--

{exp:channel:entries channel="news" limit="1"}

That way if it does fail it's a bit more graceful.

1
  • I've used that too Brian, but if there's no entry at all, I think it's better to direct to a 404 so it's clear the resulted page is not related, also with a list of entries such as in a category I can't use that. If you have any suggestions that work with EE categories out of the box I'd be very happy to hear them. :-) It's odd that I have to resort to add-ons for such a vital nuisance.
    – noregt
    Dec 10 '12 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.