I’m wanting to display categories from a weblog. I’m fairly certain that I can use either the weblog categories tag or the weblog categories archive tag, but my problem is that I’m going to have some parent categories with children under them.
These parent/child categories will be displayed as a nested list, but the parent categories will not be links, they will simply be text, with all of the children links.
I’ve tested both of the aforementioned tags, and they both do what I want to do except they both make the parent category a link.
If what I’m wanting to do isn’t possible, I believe I can do it by simply statically inserting the parent category name and then listing the child categories under the parent by using the “show=“4|7” parameter, but I’m not sure?
hmmm, yeah I believe that could work! I don’t think I would need to use the parent_only parameter in this case since I could just manually insert the parent category into the template code and then use the query from the wiki entry below it to display the children.
I’m wondering though, if anyone else has suggestions on other ways? I’ve just read in the past that when custom queries are used, the more bandwidth that is used (although it may not worth worrying about? I don’t know?)
Thanks Terry for that. If nothing else better pops up, that should work.
Actually Terry, now that I’ve had a chance to think about this solution. It might be in my best interest to use the parent_onl parameter. If I didn’t use it, I would have to manually go in and change the parent category name (if it ever changed). By using the weblog categories tag, it would all change dynamically… at least I think.
Right - using parent_only for display of the unlinked category names would give you flexibility to dynamically update any additions and changes you made to the parents categories later on. That’s what I was thinking. Then pull the children only to apply the links to them underneath each parent heading by using the sql.
Naturally, if you could do it all in a single query, it would be prettier and use fewer queries, but if the list is short to begin with and you cache your queries, it shouldn’t be much of a performance hit. And you’d retain that nice dynamic capability and not have to edit your sql ever time you added stuff.
Yeah, starting out the list will be fairly short, but it has the potential to grow large, but it would be a while down the road. How would I go about caching my queries?
<ul> {exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = '8'"} <li><a href="{path="instruction/categories/C{category_id}"}">{category_name}</a></li> {/exp:query} </ul>
My problem is that when using teh exp:weblog:category tag, I don’t see an available parameter to tell it to only show the parent with a certain ID. See, I’m going to have five, maybe six different parent categories, all with children under them. If I use the basic exp:weblog:categories tag pair with parent_only=“yes” it will display ALL of the parent categories in a list. I need to be able to specify specific ones and I’m not sure if that’s a possibility. I didn’t think about this earlier.
<ul> {exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = '8'"} <li><a href="{path="instruction/categories/C{category_id}"}">{category_name}</a></li> {/exp:query} </ul> {/exp:weblog:categories}
2. Does moving the closing tag to the bottom as I did above allow it to cycle through them, treating the parents as headers? Can’t parent_id={category_id} in the sql code? IF so, then you wouldn’t have to worry about using the category_group parameter.
I’ll think more on that and test some setups Wednesday.
You can view what I’ve got thus far: http://www.kentuckygolfing.com/instruction. When moving the closing exp:weblog:categories tag below the </ul>, the formatting is right with the parent being the <h3> heading and the list items under it, but as you can see, it’s simply listing the parent category three times, instead of listing the parent with children nested below.
I’m not sure what you’re asking about the parent_id equaling {category_id}? I’m not sure if that’s possible or why I need it?
May end up having to go with simply hard coding the parent category names into the html template or going with creating separate category groups for each of the parent categories and assigning them all to the same weblog like in this thread: http://expressionengine.com/forums/viewthread/27220/P54/
You can view what I’ve got thus far: http://www.kentuckygolfing.com/instruction. When moving the closing exp:weblog:categories tag below the </ul>, the formatting is right with the parent being the <h3> heading and the list items under it, but as you can see, it’s simply listing the parent category three times, instead of listing the parent with children nested below.
I looked and what a mess. Totally unexpected behavior - why three times??
I’m not sure what you’re asking about the parent_id equaling {category_id}? I’m not sure if that’s possible or why I need it?
I really don’t know what parent_id is being derived from. Is that its category_id? I was thinking it should draw from the same exp:weblog:categories entry as the heading, dynamically. If so, I missed one of them (it was scrolled off to the right side of my box), so both should read parent_id={category_id}.
May end up having to go with simply hard coding the parent category names into the html template or going with creating separate category groups for each of the parent categories and assigning them all to the same weblog like in this thread: http://expressionengine.com/forums/viewthread/27220/P54/
If you do, detail how you go with that to me. But I’ll try to get this working as well.
The parent_id is just taken from the category management screen under the Admin section of the CP. I don’t think you can set the parent id equal {category_id} but I may be wrong about that and there’s also that chance I’m still confused on what you’re asking. (it’s easily done).
Yeah, it seems like to me that the weblog:category_archive tag is almost identical in it’s output to the regular category tag. I just don’t see a way that I can do this with the default functionality of EE and how they next children under the parent lists. Here is the code for the weblog:category_archive tag:
The problem there is that it sets each category to <h3> (both parent and child). I need the parent to be an <h3> but the children to simply be normal sized text links. The other problem is that it generates all categories (parent and children) based on what’s between the {categories} tag pair, so if I wanted links, I would have to wrap the {category_name} in an anchor tag, which in turn would also turn the parent category into a link (which I don’t want). lol. Seems every turn I make, there is another dead end with this.
When I get some time this evening, I’m going to either set it up by hard coding the parent categories into the template and then simply using the query code from the wiki that you supplied earlier to generate the children or I’m going to create a new category group for each parent and then list the children that way. Seems to be the only way I know how to do this. The latter solution may be better (even though it offers more clutter) because at least that way, if the parent category names change, they will all update dynamically instead of me having to change them in the templates.
EDIT: NIX THIS - I HAD IT WRONG - ONLY ENTRIES LINKED - CATEGORIES DID NOT
Do you have your parents assigned to ANY items?
I ran this code just to try the category_archive tag. This is a generic categories template. I happened to have an already made category group with a bunch of child entries under two parents, and knew I had only one entry assigned. Well, without any provocation from me at all (other than there being no entries except in two categories), the parents AND the other entry-less categories were unlinked. I didn’t do anything!!!! I swear!!!
<p><a href="base1/index"><< Back to Section</a></p>
If you have parents assigned to any entries, try unassigning them entirely, and see if you get the same results. (You can do this quickly to every entry in the Edit control panel, using the drop down at the bottom left after checking the entries you want (or all entries in this case, most likely).
If it was this simple…. grrrrrrrrrrrr!!
(You called on the right man… blundering is something I do exceptionally well.)
The path (template_group/template) is used to create a URL to display this entry. This variable uses the entry’s url_title in the URL. This is typically used within a standard HTML link tag:
<a href="{path=site/index}">{title}</a>
You can also use SITE_INDEX in your path to point to your main site index page. If you show your blog on your home page, using SITE_INDEX is preferable since it will make the URL cleaner.
<a href="{path=SITE_INDEX}">{title}</a>
Since this variable uses the entry’s url title in the URL, if there is no entry, there is no entry title, and thus there is no URL!!!
Thanks for putting in all of that effort to get this worked out Terry. I appreciate that!
Yeah, with your example, it links the entries, but not to the categories themselves, which I want.
I’m going to have another search of the forums and see what I can dig up. Someone, somewhere along the line had to have needed this functionality. Seems it would be very common to me?
I haven’t had the change to try my solution yet, but I will post my results when I get the chance. I’m hoping tomorrow on my lunch break I can take a stab at it.
I just knew it had to use an embed for something. Got it working.
The query had to live in an embed and have the category_id of the parents-only category-sweep sent out to it by passing an embedded variable.
Why this stuff won’t work in-line isn’t always clear, but it sure don’t! We got proof of that!
Here is an example showing this off. The very last link, Outdoor Science Displays and Sculptures leads to a category entry possessing an actual article.
Here is the main template:
<h2 class="sidetitle">Categories</h2>
{!-- Sift out parent categories for category_id's --} {exp:weblog:categories weblog="{master_weblog_name}" parent_only="yes"}
{!-- Print Parent Name --} <h4>{category_name}</h4>
{!-- Send category_id of parent to sql query in embedded template --} {embed="base1/_embed_child_query" my_parent="{category_id}"} {/exp:weblog:categories}
Here is the embedded template, base1/_embed_child_query, with the embed:variable passed into it from the requesting template:(see way to the right parent_id={embed:my_parent} )
{!-- keep in mind there are no quotes or ticks around variables used in a SQL statement - ticks only go around absolutes --} <ul> {exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = {embed:my_parent}"} <li><a href="{path=science/C{category_id}}">{category_name}</a></li> {/exp:query} {!-- now you see why I kept wanting to put parent_id={category_id}!! --} </ul>
At long last you are paid off for your infinite patience, Sir!
And now I’m going to use this trick on my own pages!!!