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!!!
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}
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}
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
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
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}
@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
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.
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.
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.