3

I've set up three relationships in my products channel, pointing at...wait for it...the products channel. They're called also_like_1, 2 and 3.

In my main template, inside my {exp:channel:entries channel="products" limit="1"} tag, I have this:

{if also_like_1 > ''}
<div id="alsolike">
    <h3>You May Also Like</h3>
    {related_entries id="also_like_1"}
    {embed="includes/productsummary" prod="{entry_id}"}
    {/related_entries}
{/if}
{if also_like_2 > ''}
    {related_entries id="also_like_2"}
    {embed="includes/productsummary" prod="{entry_id}"}
    {/related_entries}
{/if}
{if also_like_3 > ''}
    {related_entries id="also_like_3"}
    {embed="includes/productsummary" prod="{entry_id}"}
    {/related_entries}
{/if}
{if also_like_1 > ''}
</div> <!-- END #alsolike -->
<div class="clearboth"></div>
{/if}

Here's what the productsummary template looks like:

{exp:channel:entries channel="products" id="{embed:prod}" limit="1" }
<div class="prodsumm">
{embed:prod}{title}
</div>
{/exp:channel:entries}

So, I'm getting the right values for {embed:prod}, which are the desired products entry_id's; but the {title} keeps coming out with the parent's title. :(

Main question: how do I get the desired products entry in the embedded template?

Side question: is there any way to reference the enclosing entry's fields in the inner channel:entries tag? (Either with an embed or without.)

Thanks for your help.

  • This can be accomplished much easier using Playa. You can also use one single field to select multiple entries, without needing a conditional for each additional related entry. – Jason Varga Mar 22 '13 at 18:15
  • I have Playa, and it really solved another problem I had; but I actually wanted to limit this set to 3 entries. – NotoriousWebmaster Mar 22 '13 at 18:19
  • You could limit it in the output within the template as opposed to limiting at the entry screen, if that helps. – Jean St-Amand Mar 22 '13 at 18:34
  • Thanks for the suggestion, @JeanSt-Amand, but I want to give the editor some feedback, as well. I could see them getting frustrated when they entered 4 and only 3 came up. – NotoriousWebmaster Mar 22 '13 at 18:37
  • Yeah, I supposed that's true. Maybe a limit parameter will be added to the field settings for Playa in the future. – Jean St-Amand Mar 22 '13 at 19:07
1

Your embed code is incorrect. You need to use entry_id, not id as the parameter. Also, you should use the disable parameter when embedding templates to reduce queries.

{exp:channel:entries channel="products" entry_id="{embed:prod}" limit="1" disable="member_data|pagination|categories"}
<div class="prodsumm">
    {embed:prod} - {title}
</div>
{/exp:channel:entries}

EDIT: See Jean's answer, the embed is actually not required at all.

| improve this answer | |
  • OK, made the recommended changes, and now, the embedded channel:entries tag is only returning data for the entry_id that's the same as the parent. Here's the embedded code: {exp:channel:entries channel="products" entry_id="{embed:prod}" disable="member_data|pagination|categories" } Thanks again. – NotoriousWebmaster Mar 22 '13 at 18:32
  • That's because the entries loop is being fed the ID of the parent product. With that setup, you'd still wind up needing a relationship tag pair inside the embed - so that doesn't seem quite right - which is why I suggested trying it without the embed. It may not work right though. You could try passing the parent ID to an embed that itself combines each of the three related entries into the one embed - but perhaps use a reverse related entries tag pair instead to loop through products channel and return all reverse related entries (rather than one at a time with related entries tag pair). – Jean St-Amand Mar 22 '13 at 19:13
  • "That's because the entries loop is being fed the ID of the parent product." @Jean St-Amand, no. I'm displaying the passed entry_id in the embed template and it's showing up properly. People in adjoining cubicles are wondering about the sobbing. I'm going to see if things go smoother with Playa. – NotoriousWebmaster Mar 22 '13 at 19:24
  • Ah, {embed:prod} is the entry ID of the child, is it? Yeah, you really should then be getting the custom fields from the selected child entry then. Odd. Have you tried the reverse related entries idea? A single embed conditionally inserted if a relationship exists in also_like_1, and then within that a reverse related entries loop returning all three in a single loop? – Jean St-Amand Mar 22 '13 at 19:36
2

Are you sure the embed is needed. Can relationship fields be self-referencing on their own without the need for an embed? Have you tried simply using:

{related_entries id="also_like_1"}
    {title}
{/related_entries}

Does that not generate the correct output for you? I'm so used to using playa for relationships like that, I can't remember what native relationship fields will allow you to do.

| improve this answer | |
  • You are totally correct, the embed should not be needed. – Derek Hogue Mar 22 '13 at 19:32
  • Woohoo! I thought so, but I was doubting myself a bit. – Jean St-Amand Mar 22 '13 at 19:36
  • So, this does work. But, I need an embed because the stuff that's going in there is involved, and funky, and will be used again in many other pages. But you've got me thinking about how to handle embeds. They're not just PHP includes. You can't reference fields from parent templates. I'll let you know how I make out. – NotoriousWebmaster Mar 22 '13 at 19:44
  • 1
    Try using a snippet then. Same re-usable advantage as an embed without the weight. – Jean St-Amand Mar 22 '13 at 19:46
  • My head is hurting. Yes, I'll look at snippets. Thanks for your help guys. – NotoriousWebmaster Mar 22 '13 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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