The trouble with categories...

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

The trouble with categories...

Post by TimovieMan »

Hi,

first of all: I absolutely LOVE the program!
Even with a lack of webprogramming skills, I've been able to create a site I'm very content with (style's a minor tweak of the 'bulletproof'-template).

But alas, now I seem to have hit a wall.

I'd like to have the 'categories'-plugin display only the 'parent'-categories on the frontpage and static pages. Now it displays all categories on those pages, but I have a lot of subcategories, and the list is far too long to my liking.
Is there a way to make it display only the 'depth 0' categories that doesn't require web programming knowledge?

I'd love to be able to use a self-made plugin_categories.tpl file, but everything that should go in there looks like Chinese to me, so I wouldn't know where to start...

Thanks in advance!
My (Dutch) blog about movies, life, the universe and everything... but mostly about movies: TimovieMan's Corner
onli
Regular
Posts: 2828
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: The trouble with categories...

Post by onli »

That's not too hard.
See the part in the foreach?

Code: Select all

{foreach from=$categories item="plugin_category"}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}"> 
        {if $is_form}    
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
    
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
{/foreach}
Just insert the if:

Code: Select all

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.catdepth == 0}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}"> 
        {if $is_form}    
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
    
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}
sincerely
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

Re: The trouble with categories...

Post by TimovieMan »

Thanks a bunch, that works like a charm.

Additional question about the category plugin, though. In what file can I find the original code for the category plugin (where does it get called)? Can't seem to find it...

Thanks in advance!
My (Dutch) blog about movies, life, the universe and everything... but mostly about movies: TimovieMan's Corner
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: The trouble with categories...

Post by Don Chambers »

TimovieMan wrote:In what file can I find the original code for the category plugin (where does it get called)?
The categories plugin is an internal plugin. You can find it in /include/plugin_internal.inc.php
=Don=
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

Re: The trouble with categories...

Post by TimovieMan »

Sorry to bother you guys again.
I got the categories working. When on the frontpage or the static pages, it displays only the categories of depth 0, and when in one of those categories, it displays all its subcategories. Everything as I want it, basically.

The problem arises when you select one of those subcategories. Then the code is simply:

Code: Select all

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.catdepth == 0}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}
so it should once again display all the categories with depth 0. But alas, that list remains blank.

Does this foreach loop not get called correctly when you're not in catdepth 0 or something???

See the link in my signature if you're not sure what I mean...
My (Dutch) blog about movies, life, the universe and everything... but mostly about movies: TimovieMan's Corner
onli
Regular
Posts: 2828
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: The trouble with categories...

Post by onli »

How did you achieve showing the subcategories of a selected category? I bet that get called again. f you show the whole code, we could check that.
sincerely
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

Re: The trouble with categories...

Post by TimovieMan »

I displayed those of the subcategories by showing I know jack s*** about programming... :)

I basically check the URL and then only display the categories with that URL's category as parent.

Since the subcategory-URL (like that of the frontpage and the static pages) doesn't match the URL I ask for, it goes to my 'else' loop, which contains the foreach mentioned above...

Code: Select all

{if $currpage == "http://www.timovieman.be/blog/index.php?/categories/7-home"}

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 7}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}



{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/2-Life"}

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 2}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}



{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/1-Movies"}

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 1}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}



{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/3-Reviews"}

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 3}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}



{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/52-Sharis-Corner"}

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 52}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}



{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/52-Sharis-Corner"}

{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 52}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}



{else}
{foreach from=$categories item="plugin_category"}
    {if $plugin_category.parentid == 0}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
        {if $is_form}   
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
   
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
        </li>
    {/if}
{/foreach}

{/if}
Sloppy work, I know, but I can't do better and it works for the main categories... :P
My (Dutch) blog about movies, life, the universe and everything... but mostly about movies: TimovieMan's Corner
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Re: The trouble with categories...

Post by judebert »

In the code you provided, the {else} looks for $category.parentid == 0, not $category.catdepth == 0. Now, I don't know why that would work on the frontpage, but not in subcategories. But I do know it's not what you were asking for.

I think we can clean this code up a bit, too. It'll make your template a little smaller and easier to understand. I'm busy trying it out on my sandbox; I'll let you know if I figure something out.
Judebert
---
Website | Wishlist | PayPal
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Re: The trouble with categories...

Post by judebert »

Okay, here's some simpler code for you:

Code: Select all

{* Set up the attribute to check and the value to compare against *}
{assign var='attr' value='parentid'}
{assign var='val' value='0'}
{if $currpage == "http://www.timovieman.be/blog/index.php?/categories/7-home"}
  {assign var='val' value='7'}
{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/2-Life"}
  {assign var='val' value='2'}
{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/1-Movies"}
  {assign var='val' value='1'}
{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/3-Reviews"}
  {assign var='val' value='3'}
{elseif $currpage == "http://www.timovieman.be/blog/index.php?/categories/52-Sharis-Corner"}
  {assign var='val' value='52'}
{else}
  {assign var='attr' value='catdepth'}
{/if}
{* Actually do the comparison for each category *}
{foreach from=$categories item="plugin_category"}
{if $plugin_category.$attr == $val}
<li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
{if $is_form}
<input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
{/if}

{if !empty($category_image)}
<a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
{/if}

<a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
</li>
{/if}
{/foreach}
BUT! We can make it simpler still. If you don't really want to have that giant {if} and specific categories, you can instead display the subcategories only if you're currently looking at a category view and that view is a top-level view. Just replace that HUGE {if} statement with this littler one:

Code: Select all

{if $category and $category_info.parentid == '0'}
  {assign var='val' value=$category_info.categoryid}
{else}
  {assign var='attr' value='catdepth'}
{/if}
That way, if you ever make any more top-level categories, they'll show their subcategories automatically.
Judebert
---
Website | Wishlist | PayPal
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

Re: The trouble with categories...

Post by TimovieMan »

judebert wrote:In the code you provided, the {else} looks for $category.parentid == 0, not $category.catdepth == 0. Now, I don't know why that would work on the frontpage, but not in subcategories. But I do know it's not what you were asking for.
parentid == 0 wasn't exactly what I asked for in the beginning, true, but after some experimenting, I've found that results were exactly the same whether I was looking for parentid == 0 or catdepth == 0. Which makes sense, since all categories with catdepth 0 don't have a parent category...
I think we can clean this code up a bit, too. It'll make your template a little smaller and easier to understand. I'm busy trying it out on my sandbox; I'll let you know if I figure something out.
Thank you thank you thank you thank you thank you thank you thank you thank you!!!!!!!

I tried looking for an easier method myself, but I gave up due to a lack of PHP knowledge, which is why I ended up with that massive if...elseif...elseif...etc.

Yours looks a heck of a lot better and I can now actually read it without having my head explode... :P

My plugin_categories.tpl now looks like this:

Code: Select all

{* Set up the attribute to check and the value to compare against *}
{assign var='attr' value='parentid'}
{assign var='val' value='0'}
{if $category and $category_info.parentid == '0'}
  {assign var='val' value=$category_info.categoryid}
{elseif $category}
  {assign var='val' value=$category_info.parentid}
{else}
  {assign var='attr' value='catdepth'}
{/if}

{* Actually do the comparison for each category *}
{foreach from=$categories item="plugin_category"}
{if $plugin_category.$attr == $val}
<li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
{if $is_form}
<input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" 

value="{$plugin_category.categoryid}" />
{/if}

{if !empty($category_image)}
<a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" 

alt="XML" /></a>
{/if}

<a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >

{$plugin_category.category_name|escape}</a>
</li>
{/if}
{/foreach}
As you can see, I've added an elseif statement. Results are that in the subcategories, the plugin still displays all the subcategories from the same parent as the current one.

Only odd thing left is when you select the second or third (or later) subcategory. Then it only displays all the categories STARTING with the one currently selected.

Like so:

Cat 1
- Subcat 1
- Subcat 2
- Subcat 3

When in Subcat 1, display is:
Subcat 1
Subcat 2
Subcat 3

But when in Subcat 2, display is:
Subcat 2
Subcat 3

For some reason, it doesn't display any 'earlier' categories from the same parent. But IIRC, that's something the original internal plugin did as well...

Thoughts on that???
My (Dutch) blog about movies, life, the universe and everything... but mostly about movies: TimovieMan's Corner
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Re: The trouble with categories...

Post by judebert »

That is very odd behavior. I just made the same change on my sandbox, and it displays all the sibling categories, as expected.

Perhaps our changes aren't as identical as we think. Is it possible your {if} is contained within your loop?

In the interests of conformance, here's my sandbox's entire (working) plugin_categories.tpl:

Code: Select all

{if $is_form}
<form id="serendipity_category_form" action="{$form_url}" method="post">
    <div id="serendipity_category_form_content">
{/if}

    <ul id="serendipity_categories_list" style="list-style: none; margin: 0px; padding: 0px">
{if $is_logged_in}
  {debug output="javascript"}
{/if}
{* Set up the attribute to check and the value to compare against *}
{assign var='attr' value='parentid'}
{assign var='val' value='0'}
{if $category and $category_info.parentid == '0'}
  {assign var='val' value=$category_info.categoryid}
{elseif $category}
  {assign var='val' value=$category_info.parentid}
{else}
  {assign var='attr' value='catdepth'}
{/if}
{* Actually do the comparison for each category *}
{foreach from=$categories item="plugin_category"}
{if $plugin_category.$attr == $val}
<li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}">
{if $is_form}   
<input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
{/if}

{if !empty($category_image)}
<a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" /></a>
{/if}

<a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" >{$plugin_category.category_name|escape}</a>
</li>
{/if}
{/foreach}
{* The above replaces this
{foreach from=$categories item="plugin_category"}
        <li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.categoryid}" style="display: block;">
        {if $is_form}    
            <input style="width: 15px" type="checkbox" name="serendipity[multiCat][]" value="{$plugin_category.categoryid}" />
        {/if}
    
        {if !empty($category_image)}
            <a class="serendipity_xml_icon" href="{$plugin_category.feedCategoryURL}"><img src="{$category_image}" alt="XML" style="border: 0px" /></a>
        {/if}

            <a href="{$plugin_category.categoryURL}" title="{$plugin_category.category_description|escape}" style="padding-left: {$plugin_category.paddingPx}px">{$plugin_category.category_name|escape}</a>
        </li>
{/foreach}
*}
    </ul>

{if $is_form}
    <div class="category_submit"><input type="submit" name="serendipity[isMultiCat]" value="{$CONST.GO}" /></div>
{/if}

    <div class="category_link_all"><a href="{$form_url}?frontpage" title="{$CONST.ALL_CATEGORIES}">{$CONST.ALL_CATEGORIES}</a></div>

{if $is_form}
    </div>
</form>
{/if}
You may notice that I've added a {debug} statement in there near the top. That's a trick Don Chambers showed me. It gives you a popup window with all the defined Smarty variables, so you can go through them and verify that the values you thought you had were the values you actually had. I've wrapped the statement in a {$if_logged_in}, so your casual visitors won't get the popup.

In particular, the popup window will contain a {$categories} that you may want to look at. It'll be big, but it should contain all the categories, not just the siblings that are being displayed. If it doesn't, there's a problem in the PHP somewhere.

You can give that template a shot. If it works as-is, then our templates didn't match. If it doesn't work, have a look through the {debug} output. Let me know what happens, and we'll see if we can't get this resolved.
Judebert
---
Website | Wishlist | PayPal
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

Re: The trouble with categories...

Post by TimovieMan »

I've tried your exact template, but I still have the same issue with the subcategories. The problem's got to be somewhere else, then.

The debug window doesn't list all the categories when I select either a parent or subcategory. It only lists them all when I've selected one of the static pages.

Problem is that I can't fully check your template since I'm running at a memory limit of 8 MB (which is very low - I've asked the provider to up it), so I'm close to the limit as is, and your template is often giving me a fatal error...
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Re: The trouble with categories...

Post by judebert »

TimovieMan wrote:I've tried your exact template, but I still have the same issue with the subcategories. The problem's got to be somewhere else, then.

The debug window doesn't list all the categories when I select either a parent or subcategory. It only lists them all when I've selected one of the static pages.

Problem is that I can't fully check your template since I'm running at a memory limit of 8 MB (which is very low - I've asked the provider to up it), so I'm close to the limit as is, and your template is often giving me a fatal error...
That's a little odd. The template shouldn't be using any significant amount of memory, and Serendipity gets all the categories whenever it retrieves that list.

It's possible there's a difference in our Serendipity versions, or that you've got a plugin that messes with the categories. What plugins are you running?
Judebert
---
Website | Wishlist | PayPal
TimovieMan
Regular
Posts: 8
Joined: Sat Mar 28, 2009 2:35 am

Re: The trouble with categories...

Post by TimovieMan »

Sidebar plugins:
Categories, Link List, Administration, Search, Calendar, Archive, Tags, Subscription, Powered by

Event plugins:
Markup: Serendipity/Emoticate/NL2BR/BBCode/TrackExits, Browser Compatibility, Common XHTML Error Fix, Tagging, Statistics, Extended Entry Properties, Link List, Static Pages, Contact Form

Using Serendipity version 1.4.1.

As a side note, when I delete the plugin_categories.tpl file and use the site's original category plugin, the same problem arises in the subcategories.

Possibly a PHP error somewhere???
My (Dutch) blog about movies, life, the universe and everything... but mostly about movies: TimovieMan's Corner
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Re: The trouble with categories...

Post by judebert »

If the provided template is displaying the same problem, it's certainly not the intended behavior! We expect that the {$categories} in the Smarty debug window will contain all of the categories, not just a subset of them.

I'm betting the Static Pages plugin is to blame, but I'm not ready to finger it just yet. Before I dive headlong into the code, let me verify your Category Plugins settings. The ones that are working for me on my test blog (http://tpl.judebert.com/serendipity/) are:
Fetch from: All authors
Only show categories below: All Categories
Hide selected parent: No
Sort order: Categories
Sort order: Ascending
Allow multiple: yes
Hide categories: no
Show number of entries: no
Enable Smarty: yes (duh)

If your Smarty debug still shows truncated categories when our settings are the same, I'll have to see if I can replicate your problem, then jump into the PHP and see where the category is getting truncated.
Judebert
---
Website | Wishlist | PayPal
Post Reply