Moderator’s note: Moved to Howto.
I’m having trouble figuring out how to return an error page if someone tries to access a category that doesn’t exist.
I’m using category names in URLs.
My template is at find/categories.
I’m currently using this code:
{if segment_3==''}
Code for category list goes here
{if:elseif segment_3=='category' AND segment_4==''}
There's no category listed!
<article>
{if:elseif segment_3=='category'}
{exp:channel:entries channel="weblog" limit="30" paginate="bottom" }
<header class="post-meta directory expand">
<time datetime="{entry_date format="%Y-%m-%dT%H:%i:%s%Q"}">{entry_date format="%m-%d-%y"}</time>
<h1><a href="http://{title_permalink=">{title}</a></h1>
</header>
{paginate}
<nav class="pagination">Page {current_page} of {total_pages} pages {pagination_links}</nav>
{/paginate}
{/exp:channel:entries}
</article>
{if:else}
{redirect="404"}
{/if}So, what’s going on is:
If there’s no segment_3 (which would usually be “category”) I’m going to display a list of categories.
If segment_3 IS there, but no category is requested in segment_4, I’m going to display an error message.
If segment_3 IS there, and a category requested in segment_4, I’m going to display the relevant entries
If there’s some other url structure, I’m redirecting to 404.
That all works, but I can’t figure out how to handle an invalid category request.
Currently, invalid requests (such as “.../category/this-doesnt-exist”) just display all entries.
It seems like I need something like the “no_results” operator, but that won’t work because it’s looking for a URL title, right?
Help?