We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Category Id

Development and Programming

MotoNomad's avatar
MotoNomad
32 posts
16 years ago
MotoNomad's avatar MotoNomad

Hi Laisvunas,

This is a very useful and powerful plug-in. Thank you so much for creating it.

Could anyone help me understand the “on_failure” functionality? I’m trying to throw a 404 if no category_id is found. Is this possible? no_results doesn’t seem to work here.

Any guidance would be helpful.

Should be 404 http://www.mcoscillator.org/learning_center/kb/not_a_category/

Category Index: http://www.mcoscillator.org/learning_center/kb/chart_interpretation/

Article http://www.mcoscillator.org/learning_center/kb/chart_interpretation/oscillator_breaking_zero/

(This is our development server)

Here relevant code from the learning_center/kb.php template

{!-- IF no article in segment_4 then list articles for category in segment_3 --}
{if:elseif segment_3!=''} 

    <div id="column2" class="small">
    <!--START Category Article List -->
{exp:category_id category_group="2" category_url_title="{segment_3}" parse="inward" on_failure="999999"}
    
    {exp:weblog:categories  weblog="{my_weblog}" id="{category_id}" show="{category_id}" style="linear"} 
        {if no_results}{redirect="404"}{/if}
        <h1>{category_name}</h1>
        {if category_description}{category_description}{/if}
    {/exp:weblog:categories}

    {exp:weblog:entries weblog="kb_articles" category="{category_id}"}
         {if no_results}{redirect="404"}{/if}
        <div class="topic_article">

            <div class="article_meta">
                {entry_date format="%M %d, %Y"}<br>
                {author}
            </div><!-- end article_meta -->

            <div class="article_brief">
                <h3><a href="/learning_center/kb/{segment_3}/{url_title}/">{title}</a></h3>
                 {article_brief}
            </div><!-- END article_brief -->
            <div class="clearfix"> </div>

        </div><!--END topic_article-->
    {/exp:weblog:entries}
{/exp:category_id} 
{/if}

Thanks!!!

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi MotoNomad,

Could anyone help me understand the “on_failure” functionality?

on_failure parameter allows you to specify the string which will be outputted in case category group or category url title specified in other parameters does not exist.

For example the code

{exp:category_id category_group="2" category_url_title="kb" on_failure="999999"}
{category_id}
{/exp:category_id}

will output “999999” in case there is no category group 2 or there is no category having category url title “kb” within that category group.

I’m trying to throw a 404 if no category_id is found. Is this possible? no_results doesn’t seem to work here.

If no_results conditional is quite capricious and I prefer to avoid using it. You can try to use the plugin Entries Number instrad of exp:weblog:entries tag. The code would be as follows:

{exp:category_id category_group="2" category_url_title="{segment_3}" parse="inward" on_failure="999999"}
{exp:entries_number weblog="kb_articles" category="{category_id}"}
{if entries_number==0}
{redirect="404"}
{/if}
{/exp:entries_number}
{/exp:category_id}
       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi,

I have released version 1.4 of Category Id plugin.

In this release:

1) support for single tag was ditched. Although supporting the use of single tag can save some typing in some cases, in other situations it can lead to template bugs difficult to find. So, from this release only tag pair {exp:category_id}{/exp:category_id} will be supported.

2) added support for conditionals inside exp:category_id tag. You can use conditionals to check if category exists. For example:

{exp:category_id category_group="5" category_url_title="{segment_3}"}
{if category_id!=""}
Some code
{if:else}
Another code
{/if}
{/exp:category_id}
       
Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

Hi Laisvunas,

I just finished a plugin that does the same thing! Oops, should have searched a bit more first 😉

However, I am wondering why you use the group_id as a parameter. I haven’t looked extensively, but isn’t cat_id unique and not dependent on the category group?

Great plugin, though! I think I may use yours instead (once I understand that category group part 😉 )

Thanks! Zac

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi Zac,

category_group parameter is needed because two different categories belonging to different category groups can have the same url_title.

       
Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

Spot on mate, spot on!

       
dj-rowan's avatar
dj-rowan
30 posts
16 years ago
dj-rowan's avatar dj-rowan

Great plugin- thanks for doing this! I am new to EE and I was wracking my brain to figure out how do what your plugin does!

So- i have a problem getting {category_id} to work in the exp:weblog:entries tag.

Here’s the dilemma:

{exp:category_id category_group="2" category_url_title="{segment_2}"}                                  
    category filter id: {category_id}

    {exp:weblog:entries weblog="crew-resources" dynamic="off" category="{category_id}"}                       
///some stuff///             
    {/exp:weblog:entries}
{/exp:category_id}

Now- the ‘category filter id: {category_id}’ bit outputs exactly what i want. I cannot figure out how to use this in the entries [category=”x”] parameter.

When i put in: category=”5” - my results are exactly what I want- except not dynamic… which is the whole point.

I tried:

category="{category_id}"
category={category_id}
category=category_id
category="category_id"

all to no avail!

Having the value is great- but it is kind of pointless if I can’t use it…

hope someone can sort me out!

many thanks,

Rowan

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi Rowan,

You should add

parse="inward"

to exp:category_id tag. That is, your code should be

{exp:category_id category_group="2" category_url_title={segment_2}" parse="inward"}                                  
    category filter id: {category_id}

    {exp:weblog:entries weblog="crew-resources" dynamic="off" category="{category_id}"}                       
///some stuff///             
    {/exp:weblog:entries}
{/exp:category_id}
       
dj-rowan's avatar
dj-rowan
30 posts
16 years ago
dj-rowan's avatar dj-rowan

@Laisvunas

Thank you! Oddly enough, I was just looking back through this thread and tried exactly that based on your other example. It works! WHOOHOO.. thank you so much!

I can now move along with my development!

I will read the docs to learn a bit more about the ‘parse’ parameter.

thanks again!

rowan

       
dj-rowan's avatar
dj-rowan
30 posts
16 years ago
dj-rowan's avatar dj-rowan

I see it in your first post about the plugin.

I guess to use it in the entries tag, you have to tell the plugin to look for the category_id varaible and parse it before parsing the ..:entries tag.

Very handy to know 😊

rowan

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi,

There are situations when something (say, A) should be outputted if there is certain category, but inside A also something should be outputted (say, B) if there is another category. Unfortunately, EE plugins cannot be nested, that is, the code as this would not work:

{exp:category_id category_group="5" category_url_title="{segment_3}"}
{if category_id=="10"}
Some code
{if:else}
{exp:category_id category_group="5" category_url_title="{segment_4}"}
{if category_id=="12"}
Some code
{/if}
{/exp:category_id}
{/if}
{/exp:category_id}

In order to be able to write this kind of code I released a clone of Category Id plugin - Category Id2. Its code is exactly the same as that of original plugin, except that the tag name is now {exp:category_id2} and the variable is {category_id2}.

Having installed both Category Id and its clone Category Id2 you can write the code as this:

{exp:category_id category_group="5" category_url_title="{segment_3}"}
{if category_id=="10"}
Some code
{if:else}
{exp:category_id2 category_group="5" category_url_title="{segment_4}"}
{if category_id2=="12"}
Some code
{/if}
{/exp:category_id2}
{/if}
{/exp:category_id}

and it will work correctly.

Grab the file of Category Id2 using the link in the first post in thread.

       
mhulse's avatar
mhulse
329 posts
16 years ago
mhulse's avatar mhulse

Thanks! This plugin was a life saver. 😊

       
Nate Iler's avatar
Nate Iler
29 posts
16 years ago
Nate Iler's avatar Nate Iler

Love the plugin.

I’m new to plugin development and curious to how would you go about pulling the parent_id so it can also be displayed as well? Basically I want to see if a category has a parent (or one that doesn’t equal zero) and display both id’s.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi nateiler,

Category Id plugin cannot display parent_id, but you can achieve what you wand by using together Category Id plugin and Child Categories plugin.

       
Nate Iler's avatar
Nate Iler
29 posts
16 years ago
Nate Iler's avatar Nate Iler

Thanks Laisvanas,

I understand the plugin isn’t currently able to pull the parent_id and I’m actually using it in conjunction with the child categories plugin as you mentioned to get what I need, but I was hoping I could modify the plugin to also return the parent_id so I can save a db query call. I should probably read the EE add-on documentation so I can get an understanding of whats going on with the plugin code.

       
1 2 3

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.