Conditional Sidebar Info Based on Blog Post Category

I’m not sure where to start here…

My client wants to show conditional info in the sidebar of blog posts for various categories. I want them to be able to edit the info for each category instead of it being hardcoded in.

My thought was to add custom fields to the category term page and they could edit it there.

But I’m having trouble wrapping my brain around what this loop needs to loop like… I assume it’s something similar to this? https://docs.loopsandlogic.com/105/how-to-get-if-to-work-with-a-taxonomy?category_id=8

Basically I need a loop at looks at the posts current primary taxonomy term and then if that term has the custom fields, then it displays that info…

I’m fairly close to what I need:

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

But I don’t see a way to limit it to yoast’s primary category only

I was hoping I’d be able to do something like this, but not sure what to put in the custom_field_value

<Loop taxonomy=category
      post=current
      custom_field=_yoast_wpseo_primary_category
      custom_field_value=??>
  <Field sidebar_cta_title />
</Loop>

But after a little research, looks like I need to pull this into the custom_field_value: get_post_meta(get_the_ID(), ‘_yoast_wpseo_primary_category’, true);

How can I get the current post’s ID from inside a taxonomy loop?

I just did it as a shortcode for now.

<Loop taxonomy=category
      post=current
      custom_field=_yoast_wpseo_primary_category
      custom_field_value=[primary_category_id]>
  <Field sidebar_cta_title />
</Loop>

And that is outputting the category ID of the primary category. But it is still showing all of the sidebar_cta_title for all of the categories assigned to the post…? Do custom_field=" " and custom_field_value=" " not work with taxonomy term loops?

They aren’t in the documentation as working, but I don’t know why they would have been left out of this Loop type https://docs.loopsandlogic.com/category/43

I just tested this out myself and it seems like you’re right that custom_field and custom_field_value don’t in fact exist as query parameters associated with a taxonomy term loop. I’m not familiar enough with how queries work in the backend of WordPress but it’s entirely possible that the mechanism that’s used by that query parameter when querying posts just doesn’t exist when querying taxonomy terms. If it were, I would assume it would have been included, since most of those query parameters are just piggybacking off existing PHP functions and weren’t all custom-built.

If you check out the Loop tag documentation you’ll see that there are also general-purpose attributes that you can use to filter your loop. Theoretically, they might be marginally less efficient than filtering your loop using query parameters, but they’re a lot more flexible. In your case, you could use field and field_value:

<Loop taxonomy=category
      post=current
      field=_yoast_wpseo_primary_category
      field_value="{Shortcode primary_category_id}">
  <Field sidebar_cta_title />
</Loop>

Remember that when you write a shortcode in L&L, you need to wrap it in a Shortcode tag so that it gets processed instead of just being interpreted as plain text. I haven’t worked with that feature of Yoast so I can’t tell you if using that shortcode is the right approach, but I can answer this question:

You’ve got the right idea that the current post’s ID doesn’t exist within a taxonomy term loop. You can always use variables to pass data from one loop context to another.

<Set current_id><Field id /></Set>
<Loop type=category post=current>
  <Field title />
  <Get current_id />
</Loop>

On the other hand, if you’re trying to put the current post’s ID into the value of a query parameter, you can just do that with <Field id /> because until you actually open the loop, the context of the tag is still in the current post, not in the post loop. In other words, while you’re defining your loop, you haven’t actually started the loop and therefore you haven’t changed the loop context. So something like this should work to pass the current post’s ID to the value of the field_value query parameter:

<Loop taxonomy=category
      post=current
      field=_yoast_wpseo_primary_category
      field_value="{Field id}">
  <Field sidebar_cta_title />
</Loop>

Thanks! That totally looks like it should work. But it outputs nothing :frowning:

Here’s the proof of concept. Code on the left, output on the right

So I am showing the primary category has an ID of 118. And we are looping through all tax terms assigned to this post - outputs ‘CX Hub Test’ and ‘Security Test’. And we can see ‘Security Test’ matches that ID (so that is the primary category) so that’s what we would expect it to output here:

But it outputs nothing when the Loop is filtered by it. (Shortcode is output below the Loop to show that the shortcode is outputting the correct ID)

Thoughts?

So I don’t know why it isn’t working, but I found a workaround. Since my shortcode is already figuring out the Primary Term ID for the current post, I can skip querying the ‘field_value’ all together and just pull that single term since we know the ID:

But I’d still be interested to figure out why the field and field_value couldn’t pull that info on the term Loop…

Oh, I think I see what’s going on. That _yoast_wpseo_primary_category field exists on your post, not the taxonomy term, right? And you’re trying to filter your loop down to only loop through the taxonomy term that has that number as its ID. If that’s the case, then I assume what you would actually want to write would be this:

<Loop taxonomy=category
      post=current
      field=id
      field_value="{Shortcode primary_category_id}">
  <Field sidebar_cta_title />
</Loop>

The reason your earlier attempts weren’t working was because you were trying to loop through taxonomy terms that had a _yoast_wpseo_primary_category field that matched some value. But that field doesn’t even exist on taxonomy terms in the first place.

So that explains why that template didn’t work, but I think your “workaround” is probably just the right way to go about this in the first place. As far as I can see, your _yoast_wpseo_primary_category field and your [primary_category_id] shortcode are two ways of outputting the same data, right? If that’s the case, instead of running a shortcode at all, you could probably simply use the field and avoid the extra bit of processing and complexity that’s added when you use the shortcode.

<Loop taxonomy=category terms="{Field _yoast_wpseo_primary_category}">
  <Field sidebar_cta_title />
</Loop>

Wow. I’m dumb… :man_facepalming:t3:

Sometimes you’re just too close to the problem to see what’s right in front of your face

Hahaha no worries, I also stared at it for far too long before taking a step back and realizing what the issue was! Glad we figured it out.