EEConf 2024 is around the corner! EEConf 2024
Hey everyone! Have a question in regards to do something, as well as best practices in EE in doing so!
I am working w/i a channel I created, which is my “Blog”. I have categories set up w/i the Category groups and applied the the individual entries. Everything is rendering and working great as far as displaying the entries in the list, however I am not able to figure out the correct way to grab the categories links for the individual category.
This is what I am using to get my list of categories;
<ul>
{categories}
<li><a href="http://">{category_name}</a></li>
{/categories}
</ul>
I just don’t know what to use for the <a href=”“> part. Any help is greatly appreciated! 😊
That depends on what your setup is, assuming you have Category URL setting to use Titles as links, you’d do this:
{categories}
<li><a href="{path='your_blog/index'}{category_url_title}">{category_name}</a></li>
{/categories}
If you have it set to use IDs, you’d replace {category_url_title} with {category_id}.
@Pedro, thank you for the reply! Yes, i’ve got my categories set to use the title.
{categories}
<li><a href="http://{path=your_blog/index}{category_url_title}">{category_name}</a></li>
{/categories}
For where you have ‘your_blog’ is that where I should be using {site_url}? When I did to this, it rendered the url twice. {category_url_title} along is rendering an entire url string … ie http://mywebsite.com/general (general being the category associated with this entry)
Also, this may be a dumb question, but I was under the assumption that category pages would use the index file in my template folder. That doesn’t seem like it’s the case, so with that said how would one set up a global category template? Thank you!
In most scenarios your category page will use your blog’s index template (the template you use to list entries). So if your template is named your_blog, then the path would be to your_blog/index.
Here’s an example, my own site willowlightstudio.com/news, my path is news/index.
You don’t need the http bit in the URL (it’s actually being added by the forum - a bug!), that will be added automatically.
The path value should reflect the template group which contains your blog “index” template, and the /category/category_url_title values are added automatically as well.
So if your template group is /blog:
<ul>
{categories}
<li><a href="https://expressionengine.com/blog/index">{category_name}</a></li>
{/categories}
</ul>
Will result in the URL being http://yoursite.com/blog/category/category-name
Thanks for those @Pedro & @Rob.
I guess I’m still having a little bit of an issue with stuff rendering properly. In regards to my site and how it’s here is a quick summary of exact values;
Channel: Blog Template Manager: Group: blog - With in the group blog i have; 1. Index - The overall list of ALL entries template 2. Article - The individual entries template 3. Categories - This is what I am still trying to set up. Essentially this would mimic index, but just show the entries for that particular category.
In regards to the code that you both have been helping me with, I am trying to get the code working on the “index” template (and to be used on the article and category templates as well) so that if a user clicks on that category it will take them to a categories page (ie… if a user clicked on the category “cars”, it would take them to the “cars” category page)
<ul>
{categories}
<li><a href="http://This">{category_name}</a></li>
{/categories}
</ul>
The second question is on the “categories” template, do I need or have to do anything special with entries tag to get that list?
{exp:channel:entries channel="blog" limit="4" paginate="bottom"}
MY HTML FOR ENTRIES LISTING
{/exp:channel:entries}
I thank you both for your help and continued help! 😊
@pedro that looks like im getting the correct path now w/ the following code;
<a href="http://{path=blog/index}">{category_name}</a>
So quick follow up question, if I wanted categories to go to a separate page would the code be like the following;
<a href="http://{path=blog/categories}">{category_name}</a>
Thank you so much Perdo and Rob! Your help is greatly appreciated!
In that case, you could also avoid the auto generated category URL. Though frankly, in that case I’d look into Template Routes
You could also adapt your blog/index template to use some conditionals to check what the view is, e.g. blog landing page or a category page, something like this:
{if segment_2 == "category"}
Output category entries
{if:else}
Output blog landing page
{/if}
Then you can simply point your category URLs to blog/index to give a tidier URL structure like /blog/category/category-name
@rob actually one more question in regards to your method … How would i call/grab the dynamic category information at the beginning of the list? If i wanted to show the category title and possibly the description before the list renders
“category description”
I already know i can’t call it w/i my entries tag;
{exp:channel:entries channel="blog" limit="4" paginate="bottom"}
{/exp:channel:entries}
So how or where would i put the code and what code exactly would i put?
Thank you so much!
You could use the category heading tag - https://docs.expressionengine.com/latest/channel/category_heading.html
{exp:channel:category_heading channel="blog"}
<h1>{category_name}</h1>
{if category_description}
{category_description}
{/if}
{/exp:channel:category_heading}
{exp:channel:entries channel="blog" limit="4" paginate="bottom"}
MY HTML FOR ENTRIES LISTING
{/exp:channel:entries}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.