Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Category Loop on a Category Page

Quote Reply
Category Loop on a Category Page
Hello All,
I need to loop through categories to display 4 specific ones on the category.html template. Any help will be greatly appreciated. Thank you.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Hi,

So out of all the categories , you just wanna show a few of them? This should work:

Code:
<%loop category_loop%>
<%if ID == 12 or ID = 34 or ID = 55%>
<%include subcategory.html%>
<%endif%>
<%endloop%>

The only problem with that, is that obviously the number of categories reported would be wrong in <%category_loop.length%>, but there isn't a lot you can do about that

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Hello Andy,
This would work on the index page but does not work on the category.html page as it seems that the there is no array of categories available on the category.html page.

I understand that this can be accomplished using template globals but I just do not know how to bring an array of categories to loop through using something like this:

Code:
<%loop category_loop%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%loop category_loop%>
<%if Name eq 'boo'%>
<li><a href="<%escape_html URL%>"><span><b>boo</b></span></a></li>
<%endif%>
Thank you
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Hi,

If you wanna do it via a global, try this:

tweak_category_loop
Code:
sub {
my $category_loop = $_[0];
my @new_categories;
foreach (@$category_loop) {
# Do stuff here, i.e $_->{ID}, $_->{Full_Name}, etc
#...
push @new_categories, $_;
}
return { category_loop => \@new_categories };
}

Then in category.html, do this at the first line:

<%tweak_category_loop($category_loop)%>

...and then whatever you do in the global above, should then be seen when you use <%loop category_loop%...

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Thank you Andy. Could you please provide the complete code as I am not sure what to do in the middle where you have the ....

Thank you again. I really appreciate it.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Hi,

I would need to know what you actually wanna do ;) I kinda assumed the fact you mentioned how it could be done by a global, you would know what to do Whistle

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy, I just want to bring in the name and description in this array. Thank you.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Andy, I gave it a shot but I got this error:
Quote:
Error: endloop found outside of loop

in Template Globals

Code:
sub {
my $category_loop = $_[0];
my @new_categories;
foreach (@$category_loop) {
$_->{ID}, $_->{Full_Name}, $_->{Description}
}

return { category_loop => \@new_categories };
}
in the category.html
Code:
<%tweak_category_loop($category_loop)%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>

Could you please let me know what I am doing wrong here. Thank you.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
halamalak wrote:
Andy, I just want to bring in the name and description in this array. Thank you.

Sorry, not sure what you mean? <%Name%> and <%Description%> will already exists in the <%category_loop%> array??

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy, I gave it a shot but I got this error:
Quote:
Error: endloop found outside of loop
in Template Globals

Code:
sub {
my $category_loop = $_[0];
my @new_categories;
foreach (@$category_loop) {
$_->{ID}, $_->{Full_Name}, $_->{Description}
}

return { category_loop => \@new_categories };
}
in the category.html
Code:
<%tweak_category_loop($category_loop)%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>
Could you please let me know what I am doing wrong here. Thank you.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
You are calling it wrong. The global won't work though (thats why I asked you what you were trying to actually do). Calling it should be:

Code:
<%tweak_category_loop($category_loop)%>
<%loop category_loop%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>

But as I said, that won't work anyway ( as the global is wrong). I need to actually know what you are trying to do to be able to update the global for ya Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy, what I need to do is to have access to all the Category table properties (available and in scope of category.html) as an array such that I can loop through it and choose which category to display selectively.
Thank you.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Hi,

So make a loop of ALL The categories in your database, and show them on category.html??? Still not 100% sure if thats what you're asking :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy,
Categories are not in scope of category.html.
Therefore, I can not do as it returns nothing:
Code:
<%loop category_loop%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>
With your help I was able to add the following Template Global Subroutine :

Code:
sub {
my $category_loop = $_[0];
my @new_categories;
foreach (@$category_loop) {
$_->{ID}, $_->{Full_Name}, $_->{Description}
}

return { category_loop => \@new_categories };
}
Then, I called it using the following:
Code:
<%tweak_category_loop($category_loop)%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>
When I did the above, the bullet item was returned fine. However, next to it, there was an error printed as in "Error: endloop found outside of loop"
Then, you said that the call is wrong and the correct one should be:
Code:
<%tweak_category_loop($category_loop)%>
<%loop category_loop%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>
When I did this, I the bullet item was not displayed and still got the same error: "Error: endloop found outside of loop"


Thank you for your patience and appreciate your help.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Mmm, I'm still not sure I see the problem. Doing this should work fine:
Code:
<%loop category_loop%>
<%if Name eq 'foo'%>
<li><a href="<%escape_html URL%>"><span><b>foo</b></span></a></li>
<%endif%>
<%endloop%>

(obviously thats assuming you have a category called "foo", which would show up)

Quote:
When I did this, I the bullet item was not displayed and still got the same error: "Error: endloop found outside of loop"

The reason the "wrong" code actually worked, was because it was simply printing the value of the category you are IN, not anything from the loop (as you are not defining <%loop ...%>, but instead simply printing out the values as a normal template tag)

Have you tried doing <%DUMP category_loop%> in the category.html template, to see what values you have there? That may help

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy,

<%DUMP category_loop%> on category.html returned:

Dumped value of 'category_loop':[];
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Ok, so that means you don't actually have any sub-categories in that category then???

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
OK Andy. Here is another quick explanation:

I want to have a universal navigation bar with the following items:

foo || boo || doo

These items are actually categories.

The navigation bar shows them fine on home.html

However, when I click on category (let say) USA, I will land on the category.htm page for cat USA and still want my to navigation menu printed with categories: foo || boo || doo

Thank you for your patience with me and truly appreciate your help with this.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Aaah, why didn't you say so <G>

Try my ULTRAGlobals plugin (linked in my footer), its free:

Code:
<%Plugins::ULTRAGlobals::Get_Root_Categories()%>
<ul>
<%loop root_cat_loop%>
<li><a href="<%URL%>">&nbsp;<%Full_Name%></a>(<%Number_of_Links%></li>
<%endloop%>
</ul>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy, That's Awesome. Your are amazing. Smile


I did install it and did what you said. IT WORKED!

Is there something here for subcat on home.htm as well. This is exactly the opposite. subcat is not available on home.html


Thank you a million.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Hi,

No problem :)

Quote:
Is there something here for subcat on home.htm as well. This is exactly the opposite. subcat is not available on home.html

Do you mean you want to show stuff like this on home.html:

Code:
Category
sub1 sub2 sub3

Category 2
sub1 sub2 sub3

..etc

?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Andy, apologize for the delayed response. I had to step out of the office.

Yes. You are correct. On the home page I need to get a subset of categories and specific father. Let say, Category "A" has children 1,2,3,4. I would like to get from "A" subcats 1 and 4 for example. Is there any sub in your amazing plugin that can allow me to do so?

Thank you.
Quote Reply
Re: [halamalak] Category Loop on a Category Page In reply to
Hi,

In your Plugin Manager, take a look for YahooSubCats plugin, which should do what you need :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Category Loop on a Category Page In reply to
Thank you Andy