0

I have a template that displays the categories in a channel. I want the link to direct to the first entry in that category and the link should include the url_title in the last segment as I will be needing it.

here is my code for the categories but how do I include the link to the first entry?

{exp:channel:categories channel="portfolio" show_empty="yes" style="linear"} 
<a href="{path='showroom'}"><img src="{category_image}" border="0"/></a>
<a href="{path='showroom'}">{category_name}</a>
{/exp:channel:categories}

So rather than the first link going to: http://domain.com/showroom/category/category_name

I would like it to go to: http://domain.com/showroom/category/category_name/url_title (this would be the first entry in the chosen category)

2 Answers 2

1

I believe Channel Categories only returns the category names, not the entries themselves. If you need to return a channel entry, you'll need to use the Channel Entries tag as well (or instead).

So you'll need something like

{exp:channel:categories channel="portfolio" show_empty="yes" style="linear"} 
<a href="{embed='embeds/.getEntryForCategory' cat='{category_id}'}"><img src="{category_image}" border="0"/></a> 
<!-- etc. -->
{/exp:channel:categories}

and then in the getEntryForCategory template:

{exp:channel:entries category='{embed:cat}' limit='1' dynamic='no'}{path='showroom'}/{url_title}{/exp:channel:entries}

I don't particularly like nesting templates, but I think this returns what you were after?

5
  • I think you don't need an embed for this. To set the parse parameter as inward on the exp:channel:categories tags is enough and faster to parse.
    – Sobral
    Commented Jun 13, 2013 at 19:19
  • @RobsonSobral are you saying I only have to add parse="inward" to my sample code? Commented Jun 13, 2013 at 19:30
  • You'd still need to run a channel entries I think. If you nest that inside the channel categories tag, the behaviour of 'path' changes to add category IDs into the URL (whether parse='inward' is set or not). Commented Jun 13, 2013 at 19:51
  • Also ref your answer, be careful with the {categories} in the embed. It will get all categories for the entry. You may be able to limit it, but if you want the right category using the embed solution, pass it in a parameter, e.g. {embed.... cat_name='{category_name}'}. I would have commented on your post, but don't have permission at the moment :( Commented Jun 13, 2013 at 19:54
  • You're right, @DaveForber. All variables with the same name on both categories and channel:entries will be compromised. Personally, I will just avoid them. I rarely use the path variable and I forgot about this.
    – Sobral
    Commented Jun 13, 2013 at 20:39
0

I adjusted your code @dave-forber a bit because your link did not include the category/category_name/ segments. But I am not using the Cat ID so instead I am getting the cat id when I use {embed:cat} how can I instead get the proper categopry name in the segment?

{exp:channel:entries category='{embed:cat}' channel="portfolio" limit='1' dynamic='no'}{path='showroom'}/category/{embed:cat}/{url_title}{/exp:channel:entries}

ahh this seems to work:

{exp:channel:entries category='{embed:cat}' channel="portfolio" limit='1' dynamic='no'}{path='showroom'}/category/{categories}{category_name}{/categories}/{url_title}{/exp:channel:entries}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.