0

The title may be deceiving and I may be missing something exceptionally obvious but I am trying to display a series of entries by category that link to external websites

The basic outcome should be like this:

Category 1

Category 2

I tried the basic Category Archive approach:

{exp:channel:category_archive channel="gardens" style="linear"}
    {categories}
        <h3>{category_name}</h3>
    {/categories}
        {entry_titles}
            <a href="{external_url}">{title}</a><br />
        {/entry_titles}
{/exp:channel:category_archive}

This gets the correct structure but it is looking for an internal reference and simply appends the external URL to the site url.

After some searching on StackExchange I found this option:

  {exp:channel:category_archive channel="botanical_gardens" style="linear"}
    {categories}
        <h3>{category_name}</h3>
        {exp:channel:entries channel="botanical_gardens"}
          <a href="{garden_url}">{title}</a><br />
        {/exp:channel:entries}
    {/categories}
  {/exp:channel:category_archive}

However it displays all the entries under each category. I need something that merges the functionality of these two options and allows the URL to be to an external site.

0

2 Answers 2

3

The reason the code you grabbed off SE is returning all entries under each category is that you haven't limited the inner loop by the category that the outer loop is currently in. You need to pass the category_id to the inner loop:

    {exp:channel:category_archive channel="botanical_gardens" style="linear"}
    {categories}
        <h3>{category_name}</h3>
        {exp:channel:entries channel="botanical_gardens" category="{category_id}" disable="member_data|pagination"}
          <a href="{garden_url}">{title}</a><br />
        {/exp:channel:entries}
    {/categories}
  {/exp:channel:category_archive}

The reason this approach is typically used is that the category archive tag doesn't return custom fields - which is what your URLs are stored in. This is why the field name is showing in the rendered HTML.

You'll also want to make sure you disable as much as you can on the inner loop as this nested loop approach can spawn a lot of queries. I've used it a number of times but if you are expecting 100's of categories with 1000's of entries I would look to a different organizational approach like relationships.

2
  • Hi Michael - true dedication, answering vexing EE issues early Saturday morning. This hit the spot. Fortunately I am pulling less than 100 entries, so I am not too concerned about killing the server. This is really helpful though as it is often a need to combine categories and entries in a logical stack like this. Thanks for the help.
    – forrest
    Commented Feb 21, 2015 at 14:27
  • Funny..we joke that we don't need a clock to tell us the time, we need it to tell us the day of the week..;) Between being self-employed and living on the road weekends and weekdays have little difference. Either I can and want to work or I can't or don't want to..;) Glad it helped! Commented Feb 21, 2015 at 19:00
1

Your first attempt looks correct.

What format are the urls in the field you are using? If they don't contain http:// then it's likely to be assumed they are relative to the existing site.

An extension such as VZ URL extension could help you out here by ensuring that the url stored include the protocol.

3
  • The URL displays as follows: <a href="{external_url}">'Grand Primo' Narcissus</a>
    – forrest
    Commented Feb 19, 2015 at 21:44
  • But what is stored in {external_url}? If it is simply www.somesite.com then that won't work. It needs the protocol part for a browser to properly interpret it as an external site.
    – foamcow
    Commented Feb 20, 2015 at 11:17
  • It is a fully qualified URL: example.com including the http
    – forrest
    Commented Feb 20, 2015 at 17:48

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.