1

I'm try to use related entries from different channels to display content on a home page. Using the code following with "selectNews" as a entries fieldHandle in the Home Page:

  {% set entry = entry.selectNews.first() %}
  {% if entry %}   
     ...
  {% endif %}

This places field entries from craft section "news", the first & only entry. Following that is a div with the same code, but with a new entries field and fieldHandle, "messageBanner", relating to an channel named "Banners". This second set of code fails to display anything. I see that

... {{ entry }}...

tested in the failed div, outside of {%set entry =... endif %}, prints to screen the related entry title from the previous code. How can I get this fixed and what am I missing. Pretty confused.

{% set entry = entry.selectNews.first() %}
  {% if entry %}
...
<div class="row"> 
   {% set asset = entry.newsImage.first() %}
      {% if asset %}
       <img class="col-xs-12" src=" {{ asset.url }} " alt=" News image "/> 
      {% endif %}      
    </div>
  </div>  
...
{% endif %} 


{% set entry = entry.messageBanner.first() %}
  {% if entry %}

<section class="offer-stripe center">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-12"> 
        <a href="{{ entry.bannerLink }}">
          <h5><span class="red"> some text </span><small><em> {{entry.bannerText}} </em></small></h5></a>
      </div>
    </div>
  </div>
</section>

{% endif %} 
1

1 Answer 1

2

I'm not 100% following your question, but it's likely a variable re-use issue.

Basically, you're setting the variable once, and if it exists, do blah....

Then, you're attempting to re-set that variable below. Something goes wrong with that, but entry still exists at that point, so it passes the seconds test but in that second block entry is still pointing to the data it retrieved in the first use.

(You might find it useful to check out: https://straightupcraft.com/articles/testing-if-something-exists-is-defined-length-is-not-null-is-not-empty

...which has a whole lot of stuff about testing variables too)

3
  • Thanks Jeremy for your comment, as it was a re-use issue with repetition of "entry" as a variable. I made the variable names specific to the scope of each relation. That worked.
    – bdanchuk
    Commented Mar 30, 2017 at 11:33
  • Welcome to the Craft CMS Stack Exchange, @bdanchuk! I moved your previous answer to a comment since it wasn't actually an answer. There's an FAQ article for general post guidelines if you're curious about Jeremy's earlier formatting comment or the answer-vs.-comment issue here.
    – Matt Stein
    Commented Mar 30, 2017 at 15:51
  • 1
    Also, good practise to come back and accept the answer if it helped - other will then know in the future that the answer if helpful! Commented Mar 30, 2017 at 23:05

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.