Plugin:  Category Id
Posted: 22 May 2008 01:48 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

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 - (integer starting from 1). Required. Allows you to specify id number of category group
(the id number of each category group is displayed in the Control Panel).

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 its category group is “5” 
then you can use optionally either single tag as here

{exp:category_id category_group="5" category_url_title="my_category"}

or variable {category_id} within {exp:category_id}{/exp:category_id} tag pair as here

{exp:category_id category_group="5" category_url_title="my_category"}
{category_id}
{
/exp:category_id}

Both will return id of the category which belongs to category group 5 and has URL title “my_category”.

Variable {category_id} within {exp:category_id}{/exp:category_id} tag pair is
more powerful because you can use its output 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,
the tag will output what was specified as a value of this parameter.

Place this tag in any of your templates.

Any comments and suggestions appreciated.

Changelog:

-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.

File Attachments
pi.category_id_v1.3.zip  (File Size: 3KB - Downloads: 131)
 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 28 May 2008 01:31 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

I have updated the plugin Category Id. You can download version 1.1 using the link in previous post.

In this release the bug - PHP error message being displayed when needed category id not found - was fixed.

Everyone who uses this plugin is recommended to upgrade.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 28 May 2008 07:18 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  308
Joined  01-20-2006

I do this through the query module all the time.

I’ll definitely check this out, it seems much nicer and cleaner. Excellent work.

 Signature 

AJ Penninga
Pretty Squares, LLC - http://www.prettysquares.com

Profile
 
 
Posted: 29 May 2008 05:14 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 09 June 2008 04:26 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  29
Joined  07-17-2005

Excellent. Just what I needed. Many thanks for sharing your plugin.

Profile
 
 
Posted: 09 June 2008 11:31 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1729
Joined  03-26-2006

Nice, I use the query module to do this pretty often, but this looks cool. Thanks!

 Signature 

ryan masuga
—————
Masuga Design | Member, EE Pro Network
My EE Add-Ons | {devot:ee}
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 02 July 2008 03:01 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  05-14-2004

I just want to make sure I understand how this works. smile  I want to list entries in a list grouped by category. I know I can do this with the category archive tags, but I need to list more than just the titles.

Would your plug-in accomplish this? Thanks!

 Signature 

Moxie Design Studios™ • Top Shelf Blog & Website Design by The Moxie Girls™
EE ProNetwork • (EE 1.6.5, build 20081024)

Profile
 
 
Posted: 11 July 2008 09:18 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 11 July 2008 09:27 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  05-14-2004

Thanks! smile

 Signature 

Moxie Design Studios™ • Top Shelf Blog & Website Design by The Moxie Girls™
EE ProNetwork • (EE 1.6.5, build 20081024)

Profile
 
 
Posted: 11 July 2008 09:40 AM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1729
Joined  03-26-2006

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="{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="{img_image}">{title}</a> - {custom_field_1}/{custom_field_2}</li>

   
{if count == total_results}
    
</ul>
   
{/if}

{
/exp:weblog:entries}

 Signature 

ryan masuga
—————
Masuga Design | Member, EE Pro Network
My EE Add-Ons | {devot:ee}
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 26 August 2008 09:35 AM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 17 September 2008 04:29 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  07-20-2006

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?

 Signature 

Web designer and EE Pro Network member

Profile
 
 
Posted: 17 September 2008 09:48 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

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}
<br>
{/exp:category_id}

{exp
:category_id category_group="47" category_url_title="iliad_1" on_failure="failure_output2"}
{category_id}
<br>
{/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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 18 September 2008 03:38 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  142
Joined  07-20-2006

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 wink

 Signature 

Web designer and EE Pro Network member

Profile
 
 
Posted: 12 November 2008 03:52 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  16
Joined  10-28-2007

BRILLIANT plugin, Laisvunas! I discovered it last night and it closed a missing link in EEs category management. Thank you.

Profile
 
 
Posted: 14 November 2008 05:21 PM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  11
Joined  07-14-2008

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}<p>{category_description}</p>{/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">&nbsp;</div>

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


Thanks!!!

Profile
 
 
Posted: 15 November 2008 06:00 AM   [ Ignore ]   [ # 16 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  02-02-2007

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}

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64982 Total Logged-in Users: 30
Total Topics: 82024 Total Anonymous Users: 22
Total Replies: 440857 Total Guests: 187
Total Posts: 522881    
Members ( View Memberlist )
Newest Members:  ariepChris Bandytony leodennisbaldwinhazlett_davidkpspokeli9htcluizmbentsitj