0

On wordpress, using ACF I've added a taxonomy field to the attachment to all images. I now want to display the taxonomy field in the html 'id="the-taxonomy">'. My main goal is using it as an anchor. So you'd go to my website - notreallyruby.com/work/toast#the-taxonomy.

I cannot seem to display the taxonomy tag in the html. At the moment it is just displaying the first random tag.

The html looks like this - https://paste.pics/98d4ab6b52b0951c31df62b54534b1b7 and the attachment looks like this: https://paste.pics/06d35483028b09520dc3d822d10d06e5 At the moment it says id="Apples" I want it to say "business card"

Id appreciate any help. Thank you.

<?php
add_filter('the_content','new_content');
function new_content($content) {

$term = get_queried_object();
$test = get_field('tag_cat_acf', get_post_thumbnail_id());

{

$content = str_replace('<img ', '<img data-attr="'.  $test->name .'" ', $content);
return $content; 
}
}
?>
5
  • Hi, NotReallyRuby. It's not clear to me what you're trying to accomplish. That';s the main question for me, but also, in your code,$test would have to be an object with a name property. What field type are you using, and why and how are you applying it? It's also not clear why you're trying to capture a $term object at all, since it's not used.
    – CK MacLeod
    Dec 27, 2021 at 2:57
  • @CKMacLeod hi, so using ACF I've added a taxonomy field to the attachment to all images. I now want to display the taxonomy field in the html 'id="the-taxonomy">'. My ultimate goal is using it as an anchor. So you'd go to my website - notreallyruby.com/work/toast#the-taxonomy Dec 27, 2021 at 14:47
  • I'm still not getting it, and I'm frankly not sure you understand how the various functions you're using work or are supposed to work. The images are assigned taxonomy terms ( tags or categories) different from the ones that apply to to whichever post they appear in? You want the images to work as anchor links? Why a data attribute if what you're trying to change is an ID ( leaving aside issues with that for the moment)?
    – CK MacLeod
    Dec 27, 2021 at 18:01
  • Yeah I'm fairly new to php so I'm sorry if I use the wrong terminology. The html looks like this - paste.pics/98d4ab6b52b0951c31df62b54534b1b7 and the attachment looks like this: paste.pics/06d35483028b09520dc3d822d10d06e5 At the moment it says id="Apples" I want it to say "business card" Dec 27, 2021 at 19:36
  • I think I'm beginning to understand what you want to achieve, even if I still don't understand why. It seems that what you want to do is "filter" the id attributes of specific gallery image "items" in WP Gallery Blocks. Using the_content filter will likely produce scattershot results if practicable at all. You might want to withdraw this question and create a different, more focused one. However, you might want to make sure that you want to use a a WP Gallery Block for your purposes before tunneling down this particular rabbit hole.
    – CK MacLeod
    Dec 28, 2021 at 1:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy