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]
  • #1 / Aug 18, 2011 5:01am

    This is the code I’m using..

    {exp:channel:entries channel="news" limit="3"}
        {categories}
            {if category_url_title != "{segment_3}"}
                This category does not exist
            {if:else}
                {if no_results}
                    There are no results for this category.
                {/if}
            
                <h3>{title}</h3>
    <p>            {excerpt}<br />
            {/if}<br />
        {/categories}    <br />
    {/exp:channel:entries}

    But when I try to navigate to a non-existent category this is the result I get..

    This category does not exist This category does not exist This category does not exist This category does not exist This category does not exist This category does not exist This category does not exist This category does not exist This category does not exist

    I am a bit stuck here now, can anyone please offer an elegant solution for this or suggest a plugin that will simplify things?

  • #2 / Aug 18, 2011 5:16am

    Fusion Design

    128 posts

    Hi there,

    If your segment_3 is a category id, you may be able to use the category parameter in your entries tag to suit your needs: http://ellislab.com/expressionengine/user-guide/modules/channel/parameters.html#par_category

    The following should work for you, dynamic=“no” should help as well.

    {exp:channel:entries channel="news" limit="3" category="{segment_3}" dynamic="no"}
        {if no_results}
            There are no results for this category.
        {/if}
        <h3>{title}</h3>
    <p>    {excerpt}<br />
    {/exp:channel:entries}

  • #3 / Aug 18, 2011 5:23am

    Thanks but I’m trying to use slugs with category_url_title. It looks like I can’t use them in that code in place of the ID?

  • #4 / Aug 18, 2011 5:35am

    Fusion Design

    128 posts

    Hi there,

    You should be able to use this handy add-on to convert your category_url_title to a category_id:
    http://gotolow.com/addons/low-seg2cat

    When installed, your code should look like the following

    {exp:channel:entries channel="news" limit="3" category="{segment_3_category_id}" dynamic="no"}
        {if no_results}
            There are no results for this category.
        {/if}
        <h3>{title}</h3>
    <p>    {excerpt}<br />
    {/exp:channel:entries}

  • #5 / Aug 18, 2011 5:37am

    Cheers, I will check it out.

  • #6 / Aug 18, 2011 5:45am

    Mark Bowen

    12637 posts

    Sorry in a bit of a rush and will totally admit to not having read the whole of this post fully but hopefully take a look at Cat2 as I’m pretty sure it will allow you to do what you want if I’ve read correctly which is to take a category_url_title and convert it to its corresponding category_id.

    Best wishes,

    Mark

  • #7 / Aug 18, 2011 6:47am

    Isn’t using the Category URL Indicator supposed to do the same as that anyway?

    I’m displaying the categories just fine, except I can’t find a way to reliable tell if a category does not exist and then throw an error.

  • #8 / Aug 18, 2011 8:28am

    I also found this plugin below..

    http://devot-ee.com/add-ons/category-id

    The bottom most example there looks like it might do what I want, but I’m not keen on installing or paying for plugins at every turn when but this is a pretty simple requirement.

    I also thought perhaps I could use a conditional inside the channel categories tag, but even on non-matches it seems to want to output a blank bulleted list item… and using ‘linear’ still outputs a bunch of white space in the code.

    My apologies if I’m overlooking something basic here.

  • #9 / Aug 18, 2011 8:35am

    Mark Bowen

    12637 posts

    I’m definitely running a bit slow today as I’ve got a bit of a headache from some work I’ve been doing but can I ask why you need to check if a category exists?

    What I mean by that is let’s say you have this link :

    http://www.example.com/category/shoes

    but you don’t have a category with the url_title of shoes in your system then why is anyone going to that link in the first place?

    Best wishes,

    Mark

  • #10 / Aug 18, 2011 8:54am

    I consider it best practice to avoid any unintended or broken public facing pages being served.

    I tried using the Cat2 plugin you recommended, but it causes a PHP Array to string conversion error when presented with a category_url_title segment that does not exist in the category group.

  • #11 / Aug 18, 2011 9:10am

    Mark Bowen

    12637 posts

    In that case you should just be using the :

    {if no_results}
            There are no results for this category.
        {/if}

    code if your channel tag is dynamically retrieving results from the URL then if that category doesn’t exist then there will be no results returned therefore that conditional should fire.

    You would need dynamic=“yes” in order for this to work though.

    Best wishes,

    Mark

  • #12 / Aug 18, 2011 9:21am

    I would have thought that too, but strangely I seem to get a dump of all entries for that channel instead, hence my head scratching.

  • #13 / Aug 18, 2011 9:26am

    Mark Bowen

    12637 posts

    EDIT - Actually scrap what I’ve written below. Just read your post below and not too sure how to get what you need.


    Ah I think I understand what’s happening now.

    Try adding in the :

    require_entry="yes"

    parameter to the Channel Entries Tag and see if that gets you what you need.

    Pseudo Code

    {exp:channel:entries
        channel="test"
        dynamic="yes"
        sort="asc"
        disable="member_data|pagination"}
    
    {if no_results}NO RESULTS!{/if}
    
    {title}
    
    
    {/exp:channel:entries}

    Best wishes,

    Mark

  • #14 / Aug 18, 2011 9:27am

    Ok, if I turn off Category URL Indicator/Titles and use C1,C2 etc then if I enter C3623652 for example I get the expected behaviour of no results. If I enter any other alphanumeric text not in that format I get a dump of all entries still, like I was having before.. if that helps at all!

    Just saw your above post, I’ll give it a go.

  • #15 / Aug 18, 2011 9:38am

    Adding require_entry=“yes” causes all my categories to now show the NO RESULTS message, even if there are entries in them. But it did stop non-existent categories being displayed too!

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

ExpressionEngine News!

#eecms, #events, #releases