The attached add-on files may be out of date. Please visit Devot-ee for the latest versions.
Hi,
I would like to present to your attention a new plugin - Category Id.
This plugin allows you to find category id number.
PARAMETERS:
1) category_group - Required. Allows you to specify ID number of category group (the ID number of each category group is displayed in the Control Panel). Also you can use category group name as specified in Control Panel (since v1.6). Pipe character can be used inside the value of this parameter to specify several category groups (since v1.6). In case you use pipe character, make sure category url titles are unique.
2) category_url_title - (any string which consists from characters legal for use in url segment). Required. Allows you to specify URL title of the category.
3) on_failure - (any string which consists from characters legal for use in ExpressionEngine’s tag parameter values). Optional. Allows you to specify what {exp:category_id} tag will output in case category group or category url title specified in parameters does not exist.
4) invalid_input - Optional. Accepts two values: “alert” and “silence”. Default value is “silence”. If the value is “alert”, then in cases when some parameter’s value is invalid plugin exits and PHP alert is being shown; if the value is “silence”, then in cases when some parameter’s value is invalid plugin finishes its work without any alert being shown. Set this parameter to “alert” for development, and to “silence” - for deployment.
EXAMPLE OF USAGE:
If URL title of your category is “my_category” and id number of it’s category group is “5”
then the code
{exp:category_id category_group="5" category_url_title="my_category"}
{category_id}
{/exp:category_id}
will return id of the category which belongs to category group 5 and has URL title “my_category”.
You can use output of {category_id} variable as parameter of other tag. For such use you must add parse=”inward” parameter to {exp:category_id} tag.
For example, code as this will work
{exp:category_id category_group="5" category_url_title="{segment_3}" parse="inward"}
{exp:weblog:category_archive weblog="my_weblog" show="{category_id}"}
Some code
{/exp:weblog:category_archive}
{/exp:category_id}
If category group or category url title specified in parameters does not exist then the tag {exp:category_id} by default will return nothing. In case ‘on_failure’ parameter was defined, then, in such case the tag will output what was specified as value of this parameter.
Conditionals inside {exp:category_id} tag are supported. You can use conditionals to check if category exists:
{exp:category_id category_group="5" category_url_title="{segment_3}"}
{if category_id!=""}
Some code
{if:else}
Another code
{/if}
{/exp:category_id}
Category Id2 is clone of Category Id. Having installed both Category Id and 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}
Credits: Matt Stein who updated Category Id plugin to be compatible with EE 2.0.
Changelog:
-1.4 // Ditched support for single tag. Added support for conditionals. -1.3 // Added invalid_input parameter. -1.2 // Added on_failure parameter. Fixed PHP error message being displayed when single tag was used. -1.1 // Fixed PHP error message being displayed when needed category id not found. -1.0 // First release.
I have updated the plugin Category Id. You can download version 1.2 using the link in the first post in this thread.
In this release the bug - PHP error message being displayed when single tag used - was fixed.
Also the new parameter - ‘on_failure’ - was added. This parameter is optional and accepts as its value any string which consists from characters legal for use in ExpressionEngine’s tag parameter values. It allows you to specify what {exp:category_id} tag will output in case category group or category url title specified in parameters does not exist. If category group or category url title does not exist the tag by default returns nothing. In case ‘on_failure’ parameter is defined its value will be outputted.
Why there is a need for such parameter?
Consider the code as this:
{exp:category_id category_group="5" category_url_title="{segment_3}" parse="inward"}
{exp:weblog:entries weblog="my_weblog" category="{category_id}" dynamic="off"}
<h1>{title}</h1>
{body}
{/exp:weblog:entries}
{/exp:subsegment}
If category group or category url title specified in parameters does not exist, then the value of ‘category’ parameter of {exp:weblog:entries} tag will be empty string, and {exp:weblog:entries} will output all entries from ‘my_weblog’ weblog. This may not be what you want. Most probably in case category group or category url title specified in parameters does not exist you will want that {exp:weblog:entries} tag would not output anything. This can be achieved by specifying some string as a value of ‘on_failure’ parameter. This value will become the value of ‘category’ parameter and {exp:weblog:entries} tag will not output anything.
Hi Joelle,
It seems that Category Id plugin is not what you are searching for.
You may check Entries List plugin, which has similar functionality as exp:weblog:category_archive tag.
Joelle, I do this all the time (with custom fields) without a plugin or anything. Here’s an example:
{exp:weblog:category_archive weblog="portfolio_images" style="linear"}
{categories}
{exp:weblog:entries weblog="portfolio_images" dynamic="off"
status="open" category="{category_id}" orderby="date" sort="desc"}
{if count == '1'}
<h3>{category_name}</h3>
<ul>
{/if}
<li><a href="http://{img_image}">{title}</a> - {custom_field_1}/{custom_field_2}</li>
{if count == total_results}
</ul>
{/if}
{/exp:weblog:entries}
{/categories}
{/exp:weblog:category_archive}
If that doesn’t work, you could abstract it: put the entries loop in an embed and pass the category_id that way:
{exp:weblog:category_archive weblog="portfolio_images" style="linear"}
{categories}
{embed="group/template" category="{category_id}" category_name="{category_name}"}
{/categories}
{/exp:weblog:category_archive}
Then in the embed:
{exp:weblog:entries weblog="portfolio_images" dynamic="off"
status="open" category="{embed:category_id}" orderby="date" sort="desc"}
{if count == '1'}
<h3>{embed:category_name}</h3>
<ul>
{/if}
<li><a href="http://{img_image}">{title}</a> - {custom_field_1}/{custom_field_2}</li>
{if count == total_results}
</ul>
{/if}
{/exp:weblog:entries}
Hi,
I have released a version 1.3 of Category Id plugin.
In this release parameter “invalid_input” was added. This parameter accepts two values: “alert” and “silence”. Default value is “silence”. If the value is “alert”, then in cases when some parameter’s value is invalid plugin exits and PHP alert is being shown; if the value is “silence”, then in cases when some parameter’s value is invalid the plugin finishes its work without any alert being shown. Set this parameter to “alert” for development, and to “silence” - for deployment.
Since the plugin does not do comprehensive validation of parameters’ values, you cannot be absolutely sure that in cases no alert was thrown the input was really valid.
You may ask, why parameter “invalid_input” was needed there already “on_failure” parameter being available. The answer is, these parameters have different purposes. Using parameter “invalid_input” you can specify what plugin should do in case it considers some parameter’s value being invalid; using parameter “on_failure” you can specify what plugin should do in case it considers all parameters’ values being valid, but has found nothing to output.
A much-needed plugin from my point of view. This looks like a good way to a) produce clean URLs without the word ‘category’ in them and b) create multiple levels of navigation dynamically.
I am trying this out and thought I had it working as I wanted. I have two test weblogs each with a template group and a category group. When I tried out the first weblog, all went fine. However, I have run into a weird problem (a bug even?) with the second. Basically it works unless I try to access a category with a category_url_title that already exists in the first group. Bizarrely the problem persists even after deleting the category from the first group.
The only way to make the relevant entries appear now is to rename the category_url_title in the second group - changing it to ‘test2’ makes the entries appear; changing it back to ‘test1’, they disappear.
It’s kind of frustrating as I thought I had it all worked out…
Any ideas?
Hi zackmorgan,
Trying to reproduce the issue I created two categories which belong to different category groups but have the same category_url_title and tested such code:
{exp:category_id category_group="3" category_url_title="iliad_1" on_failure="failure_output1"}
{category_id}
{/exp:category_id}
{exp:category_id category_group="47" category_url_title="iliad_1" on_failure="failure_output2"}
{category_id}
{/exp:category_id}
The output was as expected, that is, both tags outputted correct category ids.
Maybe you can provide more details concerning your problem which could help me to reproduce undesired behavior.
Hi Laisvunas,
Thanks for your quick response, and for testing this. Since posting, I tried to reproduce my navigation using native EE functionality, and ran in to problems there as well. So at the moment I am inclined to think it is nothing to do with the plugin after all, but maybe just something I don’t quite understand about categories. I hope I haven’t wasted too much of your time.
Fortunately there is always another way to do things with EE , and I did manage to implement the navigation successfully on the third attempt 😉
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.