0

I'm trying to achieve something that I think might be simple, but I'm just not getting it figured out. What I want is to have a list of my entry titles with links to anchors to the body of the entry immediately below, like this:

title1
title2
title3

body1
body2
body3

Here's the code I've got right now:

{exp:channel:entries channel="channel-name"}  
   <a href="#{url_title}">{title}</a>
{/exp:channel:entries}

{exp:channel:entries channel="channel-name"}
   <a id="{url_title}"></a>
   {body}
{/exp:channel:entries}

It seems to me that calling the channel twice is redundant, but I don't know how to get around that. Am I missing something obvious?

2 Answers 2

1

Literally, you have a fine answer. There really is no other way to get the result you want except for how you're doing it. I double up on channel entry tags in templates all the time, especially when creating image sliders and the like (having to output an image and a caption in two different places).

You may be concerned about efficiency: Sure, it's less efficient and adds the tiniest amount of extra load time. In your applications server solution, however, you can really solve this issue with fantastic products like CE Cache or Cloudflare.

http://www.causingeffect.com/software/expressionengine/ce-cache

http://www.cloudflare.com/

You aren't even going to notice a page load issue with two channel entries tags (if your templates are intelligently designed, at least).

3

The only sensible way to do this with a single channel entries loop is to use Stash and create one or more Stash lists within your loop. Then outside the entries loop use the Stash list to output the titles, followed by the contents.

I think it would go something like:

First create the Stash list variables (there are a couple of ways to do this)

{exp:channel:entries channel="foo" dynamic="no"}
    {exp:stash:append_list name="my_list"}
        {stash:item_title}{title}{/stash:item_title}
        {stash:item_url_title}{url_title}{/stash:item_url_title}
        {stash:item_content}{content}{/stash:item_content}
    {/exp:stash:append_list}
{/exp:channel:entries}

Then output the list twice, getting different bits of it each time.

{exp:stash:get_list name="my_list"}
    <a href="#{stash:item_url_title}">{stash:item_title}</a>
{/exp:stash:get_list}

{exp:stash:get_list name="my_list"}
   <div id="{stash:item_url_title}">{stash:item_content}</div>
{/exp:stash:get_list}

To be honest, the way you've done it is probably what I would do unless I was running into performance issues in which case Stash might be of help.

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.