Thread

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

January 04, 2017 12:09pm

Subscribe [1]
  • #1 / Jan 04, 2017 12:09pm

    drive.andy's avatar

    drive.andy

    21 posts

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

  • #2 / Jan 04, 2017 12:17pm

    Pedro Guimaraes's avatar

    Pedro Guimaraes

    163 posts

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

  • #3 / Jan 04, 2017 12:53pm

    drive.andy's avatar

    drive.andy

    21 posts

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

  • #4 / Jan 04, 2017 1:15pm

    Pedro Guimaraes's avatar

    Pedro Guimaraes

    163 posts

    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.

  • #5 / Jan 04, 2017 1:22pm

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    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

  • #6 / Jan 04, 2017 1:44pm

    drive.andy's avatar

    drive.andy

    21 posts

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

  • #7 / Jan 04, 2017 1:51pm

    Pedro Guimaraes's avatar

    Pedro Guimaraes

    163 posts

    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.

  • #8 / Jan 04, 2017 1:56pm

    drive.andy's avatar

    drive.andy

    21 posts

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

  • #9 / Jan 04, 2017 2:11pm

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    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

  • #10 / Jan 04, 2017 2:13pm

    Pedro Guimaraes's avatar

    Pedro Guimaraes

    163 posts

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

  • #11 / Jan 04, 2017 2:16pm

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    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

  • #12 / Jan 04, 2017 2:29pm

    drive.andy's avatar

    drive.andy

    21 posts

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

  • #13 / Jan 04, 2017 2:37pm

    drive.andy's avatar

    drive.andy

    21 posts

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

  • #14 / Jan 11, 2017 6:45pm

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    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}

ExpressionEngine News

#eecms, #events, #releases