Using L&L Inside a Category Archive

Ah, you want to sort the output

Yeah, I want a loop in a loop.

Outside loop is child taxonomy. Inside loop is posts inside that child taxonomy.

Like I said, this works outside of the archive to do just that:

<Loop type=taxonomy_term taxonomy=local_category>
  <h2><Field title /></h2>
  <Loop type=local_favorite taxonomy=local_category terms="{Field id}">
    <Field title />
  </Loop>
</Loop>

Now I need to figure out how to do that to pull the child taxonomies on that archive page
Which is why I thought something like this would work:

<Loop type=taxonomy_term>
  <h2><Field title /></h2>
  <Loop>
    <Field title />
  </Loop>
</Loop>

Too complicated for me.

In case it helps a little, i got this that lists all the top level Category terms, then the children terms under each:

<Loop type=taxonomy_term taxonomy=category parents=true>
  <Field title /><br>
  <Loop field=children>
    – <Field title /><br>
  </Loop>
</Loop>

Returns something like:

Cat 1
Cat 2
– Cat 2.1
Cat 3

Cat 2.1 being a child of Cat 2

L&L misses so much examples in the doc compared to CCS! :pensive:

Thanks for giving it a go! Hopefully @julia has an answer :slight_smile:

1 Like

I found something new to output the current Archive term:
field=archive_term

<Loop field=archive_term parents=true>
  <Field title /><br>
  <Loop field=children>
    – <Field title /><br>
  </Loop>
</Loop>

Which will return, if you’re in the Cat 2 Archive:

Cat 2
– Cat 2.1

The current Archive term and its child terms

I find confusing to have a field attribute in the <Loop> tag…

Wow. That’s getting closer. I just tried to put a loop in a loop in a loop, but that didn’t work :rofl:

This is what I did:

<Loop field=archive_term>
  <Loop field=children>
    <h2><Field title /><br></h2>
    <Loop>
      <Field title /><br>
    </Loop>
  </Loop>
</Loop>

And this is what it output - same posts under each child tax term… :man_shrugging:t3:

I made the same test, unfortunately, the nested loop returns all posts of the current Archive, no way to exclude/include posts having the parent or child term. :wink:

L&L mysterious way…

Hi Zach,

I’m sorry I wasn’t able to respond sooner. Could you see if the following code gets closer to what you need?

<Loop field=archive_term>
  <Loop field=children>
    <h2><Field title /></h2>
    <Loop field=posts>
      - <Field title /><br>
    </Loop>
  </Loop>
</Loop>

The first loop gets the current taxonomy term in the category archive page. The second loop gets that term’s children.

The third loop gets posts that belong to each child term. This is a newly added feature in the plugin version published this week. It’s equivalent to:

<Loop type=post taxonomy="{Field taxonomy}" term="{Field id}">

Thanks for taking a look. I updated and tried your suggestion, but it’s only outputting the taxonomy:
Screen Shot 2022-04-27 at 12.57.23

This was the closest I got before: Using L&L Inside a Category Archive - #18 by zack

I see, so <Loop field=posts> is not working as expected. Hmm, I wonder why that’s empty…

Just to clarify, in this expected result:

Breakfast

  • place 1
  • place 2

Do the posts “place 1” and “place 2” have the child term called “Breakfast”, or do they only have the parent term “Restaurants”?

Yes, that is the correct expected result.

Breakfast

  • place 1
  • place 2

Lunch

  • place 3
  • place 4

Dinner

  • place 5
  • place 6

And each of those have the child term (so place 1 has Breakfast as a term)

One thing that could have a difference is that I have removed the parent term from each, so none of them have Restaurant as a parent term. They only have the child term.

Oh, I forgot to mention, the field=posts is a newly added feature in the latest plugin version 2.4.1. Could you check and update the plugin if necessary?

EDIT: Oops, never mind, I see you did update the plugin. OK, I’ll dig deeper and see if I can figure out why it’s not working as excepted.

Hi Zack,

I figured out that Loop field=posts was getting posts belonging to a child term, but only from the post type post. In the newest plugin version, this has been changed so it gets posts from any post type.

So the following should work now:

<Loop field=archive_term>
  <Loop field=children>
    <h2><Field title /></h2>
    <Loop field=posts>
      - <Field title /><br>
    </Loop>
  </Loop>
</Loop>

I made other improvements related to taxonomy query, taxonomy term loop, and better support for taxonomy archive page.

That includes terms=current for the post loop, which you tried before. It should work now too:

<Loop type=local_favorite taxonomy=local_category terms=current>
  <Field title />
</Loop>

Wonderful. That worked :raised_hands:t2: Thank you for working on that!

1 Like

Hi @eliot ,

Can you figure out why I’m getting a count of 0 for the first loop of field=archive_term?
(the “Archive Children Count” output)
It should be the number of taxonomy terms, correct?

<Loop field=archive_term>
  <div>Archive Children Count: <Field count/></div>
  <Loop field=children>
    <br>
    <div>Loop Count: <Field count/> </div>
    <h2><Field title /></h2>
    <Loop field=posts>
      - <Field title /><br>
    </Loop>
  </Loop>
</Loop>

Use case: The above code works for archives with children taxonomy terms. But it shows nothing if you’re on the lowest child. And I’m using this code on my taxonomy archive template. So I was going to use an if statement using the count to determine which html to render.

Hey Zack, I read through this thread but I’m honestly not totally clear on exactly how field=something syntax works here. I’ll have to look into that. In any case, I think when you initially started this thread there weren’t as many parameters available for filtering taxonomy term loops, but there’s now a few that you should be able to use to achieve what you’re trying to do. In your case I think the ‘parent’ attribute is the one you’d want to use, similar to what I recommended in this thread earlier today. It would allow you to create an inner taxonomy term loop that loops through only the immediate child terms of the current parent from the outer taxonomy term loop. You can nest those loops as deep as your site’s taxonomy structure requires. Let me know if that doesn’t fit your need here and I can look into other suggestions.

@benjamin - Yeah, @eliot posted with the syntax for that. According to them, it was added in v2.4.1.

Well, my main problem I have at this point is that I have one archive template that I want to show EITHER:

1. The sub categories of the current page’s taxonomy. (Ex: Restaurant taxonomy shows Breakfast, Lunch, & Dinner sub categories of Restaurant)

Breakfast

  • place 1
  • place 2

Lunch

  • place 3
  • place 4

Dinner

  • place 5
  • place 6

OR 2. If the current page is already the lowest level taxonomy (aka, there is only one taxonomy item on the page - like it was already in the Breakfast taxonomy example from above), then just this:

  • place 1
  • place 2
  • place 3
  • place 4
  • place 5
  • place 6

So my plan was to use an IF statement to do two different loops depending of if there were more than 1 child in the loop or not. But that’s not working because is outputting 0 for the

Maybe there’s a better way? Idk. Anyway, here’s what I have that is not working:

<div id="local-favorite-grids">
  <Loop field=archive_term>
    <Loop field=children>
      <If count more_than_or_equal value="2">
        <div>
          <h2 class="favorite-subcategory" id="{Field name}">
            <Field title />
          </h2>
          <div class="local-favorite-grid">
            <Loop field=posts>
              <div class="local-favorite-card">
                <div class="fl-post-image">
                  <img aria-hidden="true" class="local-featured-image" src="{Field image_url}" srcset="{Field image_srcset}" sizes="{Field image_sizes}">
                </div>
                <div class="fl-post-text">
                  <h3 class="favorite-name">
                    <Field title />
                  </h3>
                  <div class="favorite-meta">
                    <div class="favorite-location">
                      <img class="favorite-location-pin" aria-hidden="true" src="/wp-content/uploads/2022/02/teg-map-marker.png">
                      <div aria-label="{Field title} Location">
                        <Field name=favorite_city>
                      </div>
                    </div>
                    <div class="local-category">
                      <Taxonomy local_category orderby=menu_order>
                        <a href="{Field url}" class="local-cat-link">
                          <Field title />
                        </a><span class="local-cat-sep">,</span>
                      </Taxonomy>
                    </div>
                  </div>
                  <a href="tel:{Field name=favorite_phone_number}" class="favorite-phone">
                    <Field name=favorite_phone_number>
                  </a>
                  <If field="favorite_url" exists>
                    <a href="{Field name=favorite_url}" class="fl-button" target="_blank"><span class="fl-button-text">Visit Site</span></a>
                  </If>
                </div>
              </div>
            </Loop>
          </div>
        </div>
      <Else />
        <div>
          <Loop field=children>
            <h2 class="favorite-subcategory" id="{Field name}">
              <Field title />
            </h2>
            <div class="local-favorite-grid">
              <Loop field=posts>
                <div class="local-favorite-card">
                  <div class="fl-post-image">
                    <img aria-hidden="true" class="local-featured-image" src="{Field image_url}" srcset="{Field image_srcset}" sizes="{Field image_sizes}">
                  </div>
                  <div class="fl-post-text">
                    <h3 class="favorite-name">
                      <Field title />
                    </h3>
                    <div class="favorite-meta">
                      <div class="favorite-location">
                        <img class="favorite-location-pin" aria-hidden="true" src="/wp-content/uploads/2022/02/teg-map-marker.png">
                        <div aria-label="{Field title} Location">
                          <Field name=favorite_city>
                        </div>
                      </div>
                      <div class="local-category">
                        <Taxonomy local_category orderby=menu_order>
                          <a href="{Field url}" class="local-cat-link">
                            <Field title />
                          </a><span class="local-cat-sep">,</span>
                        </Taxonomy>
                      </div>
                    </div>
                    <a href="tel:{Field name=favorite_phone_number}" class="favorite-phone">
                      <Field name=favorite_phone_number>
                    </a>
                    <If field="favorite_url" exists>
                      <a href="{Field name=favorite_url}" class="fl-button" target="_blank"><span class="fl-button-text">Visit Site</span></a>
                    </If>
                  </div>
                </div>
              </Loop>
            </div>
          </Loop>
        </div>
      </If>
    </Loop>
  </Loop>
</div>

I’m actually, even unsure if my IF statement is nested correctly not not, but that’s the only place that anything outputs at all. I think if the archive loop was outputting the correct count, then swapping the and the IF statement would be correct?

Thanks for the DMed link to your site. Looking into it a bit more, it seems that what you’re doing with that field=something syntax is that you’re creating a field loop. It seems that the children field of a WordPress taxonomy term contains a list of all of that term’s children. You could certainly work with that, but you’re sorta working “blind” in that you’re looping through the contents of a field that you can’t see/control and you also don’t have access to all the other query parameters that make L&L so powerful. You could definitely do it that way, but I think it would be easier to create a taxonomy term loop each time so that you know what you’re looping through and are able to use things like If loop exists.

I think using a taxonomy term loop with the parent attribute (instead of a field loop as you’ve done) is going to be your best bet here, so here’s how I’d approach this. I don’t think your outer <Loop field=archive_term> part is necessary since I assume that the default loop of the page is already querying the current taxonomy term.

<Loop type=taxonomy_term taxonomy=your_taxonomy_name parent="{Field id}"> <!-- This is looping through all the child terms of the current taxonomy term, which I think is the main loop that you're looking for -->
  <If loop exists type=taxonomy_term taxonomy=your_taxonomy_name parent="{Field id}"> <!-- This checks whether there are any child terms under the current term of the outer loop -->
    <Field title /> <!-- If there are child terms, then we display the title of the parent term on this line and then loop through the child terms and their titles below. Obviously, you'd replace this with your styled markup, but this should at least show you the right data/structure -->
    <Loop>
      <Field title />
    </Loop>
  <Else /> <!-- If the current term doesn't have its own child terms, we'll just display the term title -->
    <Field title />
  </If>
</Loop>

Hope that makes sense and works in your case! Let me know if I’ve missed/overlooked anything in your request.

Sorry, that doesn’t output anything… :man_shrugging:t3: