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.

How can I check if a category exists?

August 18, 2011 5:01am

Subscribe [5]
  • #16 / Aug 18, 2011 11:39am

    the3mus1can

    426 posts

    Try something like this:

    {exp:query sql="SELECT cat_id FROM exp_categories WHERE cat_url_title = '{segment_2_or_whatever}' LIMIT 1"}
        
        {exp:channel:entries
            channel="your_channel"
            disable="categories|category_fields|member_data|pagination|trackbacks"
            dynamic="off"
            category="{cat_id}"
        }
            
            ... other awesome code here ...
                        
        {/exp:channel:entries}
        
        {if no_results}
            That is not a valid category.
        {/if}
        
    {/exp:query}

    You might need to tweak it a little, but it is a starting place.

  • #17 / Aug 18, 2011 11:57am

    Thank you the3mus1can, your solution is working for me now and I can get my project moving again.

  • #18 / Aug 18, 2011 1:03pm

    Pagination does not seem to be working now, odd. I have enabled it, and added inside the channel entries closing tag.

  • #19 / Aug 18, 2011 1:06pm

    the3mus1can

    426 posts

    Yeah, you will probably have to put the entries loop into an embed and pass in the cat_id.

  • #20 / Aug 18, 2011 1:13pm

    Fantastic, thanks!

  • #21 / Nov 16, 2011 6:13am

    artminister

    159 posts

    You could also use low seg2cat to check if a category in the url exists.

    {if segment_2_category_id == ""}
      {redirect="404"}
    {/if}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases