Check for posts inside a loop and add a link

Hey there, I am having a problem and am curious if there is a solution for it. Inside a CPT “team” I have a taxonomy “contactpersons” with some taxonomy terms. In another CPT “groups” I have posts – some of them have corresponding titles to the contactperson’s terms – and I am trying to add links if there are matching posts. I tried:

<div class="team-meta">
<Taxonomy contactpersons>
<If loop exists type=groups name="{Field name}">
  <Loop>
    <a href="{Field url}">
      <Term title />
    </a>
  </Loop>
<Else />
</If>
</Taxonomy>
</div>

The loop is working but breaks after the first post. If I strip the part <Taxonomy>…</Taxonomy> all “team” posts are shown in the frontend. Any idea why? Template is loaded in the content of the “team” posts with a shortcode. All “team” posts are shown by the theme.

Hi Daniel! I think your issue probably lies in your use of <Term title />. The Term tag only exists within the Taxonomy tag but by the time you use it in your template, you’ve changed the loop context and it’s used inside a regular Loop tag. You might try changing this to the more standard syntax, <Field title /> to display the title of the current groups post you’re looping through.

One other thing to note is that the <Else /> tag doesn’t seem to be necessary in your case, but that shouldn’t be causing the issue you’re describing, I just thought I’d mention it.

Another thing to check if my suggestion above doesn’t fix things, you mention that the titles match, but in your example you’re using Field name which is basically the post/term’s slug, not its title. Are you sure that the slugs match between the taxonomy term and the group?

Hey Benjamin, thank you for your fast response! I tried your solutions with no success.

The code works perfectly fine if I am in a single post of the CPT “team”. I trigger it with a shortcode placed in the content field of each “team” post. But if I am in the “team” archive, where all posts with their content are displayed, it crashes the archive and only the first post is shown (with correct layout and syntax, but all other posts are missing). Any idea why?

Ah, understood, that makes a lot more sense then. You might want to read through this blog post to understand the idea of loop context and how that affects the way the template works. The gist of it is that the <Taxonomy contactpersons> line of your template is designed to loop through all the contactpersons taxonomy terms that are associated with the current post. On a single post page, it’s pretty obvious what the “current” post is. But on an archive page, the loop context of the archive page is ALL the team posts. So L&L tries to look for the “current” post, finds the first one, displays the Taxonomy loop for that one post and calls it a day. The tag seems to be working exactly as it’s designed to work.

The solution here really depends on how you’re placing your template on your archive page and whether your whole archive page is built using L&L. If the archive page is built with L&L, then you just need to make sure that the template you shared in this thread is located within your “team” post loop so that it runs once for each post in the archive. If your archive page isn’t built with L&L, you’ll need to clarify how it’s built and how you’re currently placing the template onto the archive.

Hey Benjamin, I think the solution lies within your last sentence because I am mixing parts of the functionality of the theme used for the installation and am only adding L&L to it. Will try to build the whole archive with L&L and it should work in the end.

Can I somehow get the associated taxonomy term of the current post as field? Without using a Loop? Something like:

<Field term />

Definitely!

Nope!

Unfortunately, this is just down to the way WordPress saves its data. Some data is saved directly within a post, such as the post’s title or content. Other data only exists on the post as an ID number so that WordPress knows where to look to grab that data. For example, when you create an image field in ACF and then upload an image to that field on a post, you’re not adding the image directly to the post; the only data saved in the post is the attachment ID of that image, which is why you need to use a loop to display image custom fields. It’s the same idea with taxonomy terms. We often think of terms as just being a single string of text, but taxonomy terms exist in their own location in the WordPress database and can have all sorts of data associated with them (even custom fields). There can also be multiple terms associated with a single post, which is why displaying that information using a single Field tag wouldn’t be possible or even make sense in a lot of cases. Even if you could display it, the only taxonomy term data that WordPress stores on a post is the term IDs so writing something like <Field term /> (if that field existed in L&L) would output a string of associated term IDs, something like 12,56,24,90, which wouldn’t be very useful. I sort of alluded to that in my response in this thread when I said:

All that to say that you’ve always got to use a loop to get information related to a post’s taxonomy terms. Luckily, it’s as simple as:

<Loop taxonomy=category post=current>
  <Field title />
</Loop>

Thanks for getting back to me. That is how I understood it beforehand but I wanted to get it totally clear for me. As I already said I will rebuild my archive using L&L and everything will be fine. Tested it already.

1 Like