We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Channel Entries - How to show the categories for an individual entry

How Do I?

andrew.reff's avatar
andrew.reff
26 posts
8 years ago
andrew.reff's avatar andrew.reff

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! 😊

       
Pedro Guimaraes's avatar
Pedro Guimaraes
170 posts
8 years ago
Pedro Guimaraes's avatar Pedro Guimaraes

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

       
andrew.reff's avatar
andrew.reff
26 posts
8 years ago
andrew.reff's avatar andrew.reff

@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!

       
Pedro Guimaraes's avatar
Pedro Guimaraes
170 posts
8 years ago
Pedro Guimaraes's avatar Pedro Guimaraes

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.

       
Rob Allen's avatar
Rob Allen
2,950 posts
8 years ago
Rob Allen's avatar Rob Allen

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

       
andrew.reff's avatar
andrew.reff
26 posts
8 years ago
andrew.reff's avatar andrew.reff

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 Guimaraes's avatar
Pedro Guimaraes
170 posts
8 years ago
Pedro Guimaraes's avatar Pedro Guimaraes

Rob is correct that you don’t need {category_url_title} or {category_id}. The actual code I use is:

<a href="{path='news/index'}">{category_name}</a>

There’s no need to alter your channel entries tag.

       
andrew.reff's avatar
andrew.reff
26 posts
8 years ago
andrew.reff's avatar andrew.reff

@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!

       
Rob Allen's avatar
Rob Allen
2,950 posts
8 years ago
Rob Allen's avatar Rob Allen

In that case you could create a template called “categories” and output your data there. However a generated categorv URL would then be /blog/categories/category/my-category-name

       
Pedro Guimaraes's avatar
Pedro Guimaraes
170 posts
8 years ago
Pedro Guimaraes's avatar Pedro Guimaraes

In that case, you could also avoid the auto generated category URL. Though frankly, in that case I’d look into Template Routes

       
Rob Allen's avatar
Rob Allen
2,950 posts
8 years ago
Rob Allen's avatar Rob Allen

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

       
andrew.reff's avatar
andrew.reff
26 posts
8 years ago
andrew.reff's avatar andrew.reff

@Pedro & @Rob thank you both so much for your help!!!

@rob yep, that was the idea that came to mind and working like a charm! Thank you both!

       
andrew.reff's avatar
andrew.reff
26 posts
8 years ago
andrew.reff's avatar andrew.reff

@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 title

“category description”

  • My list of entries.
  • My list of entries.
  • My list of entries.

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!

       
Rob Allen's avatar
Rob Allen
2,950 posts
8 years ago
Rob Allen's avatar Rob Allen

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}
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.