Skip to main content
re-indented for clarity
Source Link
Marion Newlevant
  • 12.1k
  • 1
  • 23
  • 55

I'm creating a dynamic side navigation where pages can choose which sidebar it wants to use. This is selected by an entries field called 'sidebar' - called in line 2.

To create this I've made a structure called 'Sidebars' with an entry for each sidebar type. Each Sidebars entry has an entries field in it which can select any number of pages.

I've got it fully working, but it's giving me deprecation errors.

Here's the working (but deprecated code on line 4): (lines 5-12 figure out whether the path includes 'whats-on' or 'who-we-are' and adds them to the path to the entry if needed).

  <div>
    {% set sidebarChoice = entry.sidebar.one() %}
    {% for rel in sidebarChoice %}
    {% for sidebarEntry in rel %}
      {% set myEntryQueryUrl = sidebarEntry.url %}
      {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
      {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
      {% else %}
        {% set myEntryQueryPath = '/' %}
      {% endif %}
 

        <div>
            <h3>{{ sidebarEntry.title }}</h3>
            <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
            <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
            </a>
        </div>
            
            {% endfor %}
            {% endfor %}
      </div>

I'm creating a dynamic side navigation where pages can choose which sidebar it wants to use. This is selected by an entries field called 'sidebar' - called in line 2.

To create this I've made a structure called 'Sidebars' with an entry for each sidebar type. Each Sidebars entry has an entries field in it which can select any number of pages.

I've got it fully working, but it's giving me deprecation errors.

Here's the working (but deprecated code on line 4): (lines 5-12 figure out whether the path includes 'whats-on' or 'who-we-are' and adds them to the path to the entry if needed).

  <div>
    {% set sidebarChoice = entry.sidebar.one() %}
    {% for rel in sidebarChoice %}
    {% for sidebarEntry in rel %}
    {% set myEntryQueryUrl = sidebarEntry.url %}
    {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
    {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
    {% else %}
        {% set myEntryQueryPath = '/' %}
    {% endif %}
 

        <div>
            <h3>{{ sidebarEntry.title }}</h3>
            <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
            <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
            </a>
        </div>
            
            {% endfor %}
            {% endfor %}
      </div>

I'm creating a dynamic side navigation where pages can choose which sidebar it wants to use. This is selected by an entries field called 'sidebar' - called in line 2.

To create this I've made a structure called 'Sidebars' with an entry for each sidebar type. Each Sidebars entry has an entries field in it which can select any number of pages.

I've got it fully working, but it's giving me deprecation errors.

Here's the working (but deprecated code on line 4): (lines 5-12 figure out whether the path includes 'whats-on' or 'who-we-are' and adds them to the path to the entry if needed).

<div>
  {% set sidebarChoice = entry.sidebar.one() %}
  {% for rel in sidebarChoice %}
    {% for sidebarEntry in rel %}
      {% set myEntryQueryUrl = sidebarEntry.url %}
      {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
      {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
      {% else %}
        {% set myEntryQueryPath = '/' %}
      {% endif %}

      <div>
        <h3>{{ sidebarEntry.title }}</h3>
        <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
        <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
        </a>
      </div>

    {% endfor %}
  {% endfor %}
</div>
Source Link

How to loop through multiple entries fields

I'm creating a dynamic side navigation where pages can choose which sidebar it wants to use. This is selected by an entries field called 'sidebar' - called in line 2.

To create this I've made a structure called 'Sidebars' with an entry for each sidebar type. Each Sidebars entry has an entries field in it which can select any number of pages.

I've got it fully working, but it's giving me deprecation errors.

Here's the working (but deprecated code on line 4): (lines 5-12 figure out whether the path includes 'whats-on' or 'who-we-are' and adds them to the path to the entry if needed).

  <div>
    {% set sidebarChoice = entry.sidebar.one() %}
    {% for rel in sidebarChoice %}
    {% for sidebarEntry in rel %}
    {% set myEntryQueryUrl = sidebarEntry.url %}
    {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
    {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
    {% else %}
        {% set myEntryQueryPath = '/' %}
    {% endif %}


        <div>
            <h3>{{ sidebarEntry.title }}</h3>
            <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
            <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
            </a>
        </div>
            
            {% endfor %}
            {% endfor %}
      </div>