【问题标题】:Display acf taxonomy attachment the_content显示 acf 分类附件 the_content
【发布时间】:2021-12-30 20:19:08
【问题描述】:

在 wordpress 上,我使用 ACF 在所有图像的附件中添加了一个分类字段。我现在想在 html 'id="the-taxonomy">' 中显示分类字段。我的主要目标是使用它作为锚。所以你会去我的网站 - Fakewebsite.com/work/toast#the-taxonomy。

我似乎无法在 html 中显示分类标记。目前它只是显示第一个随机标签。 (苹果)

html 看起来像这样 - https://paste.pics/98d4ab6b52b0951c31df62b54534b1b7,附件看起来像这样:https://paste.pics/06d35483028b09520dc3d822d10d06e5 现在它说 id="Apples" 我希望它说“名片”

感谢您的帮助。谢谢。

<?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;
    }
}
?>

【问题讨论】:

  • 你为什么得到这个词?
  • 我不太确定哪个图像包含该字段。您正在使用get_post_thumbnail_id(),但我认为除非您的主题正在这样做,否则我认为它不会通过the_content,尽管我可能在这方面是错误的。
  • @HowardE 嗨,我应该得到什么?对不起,我是 php 开发的新手。谢谢
  • @ChrisHaas 您好,您有什么建议吗?我尝试了很多组合,而这种组合似乎是唯一真正显示某些东西的组合(我是 php 开发的新手)。谢谢
  • 我应该问...你为什么得到查询的对象?获取帖子缩略图ID的目的是什么? ACF 字段是否与分类、帖子或媒体项目相关?

标签: php html wordpress advanced-custom-fields


【解决方案1】:

我想我明白你在寻找什么,而且我认为你的方法不是我想要的。

我使用了这个过滤器wp_get_attachment_image_attributes。它允许您向媒体项目添加属性。然后,您可以使用此过滤器获取媒体项 id,并检索 ACF 字段。

add_filter( 'wp_get_attachment_image_attributes', 'dd_custom_attribute_acf', 10, 2 );
/**
 * Filter the media item to add an ACF data-attribute to an image.
 *
 * @param array  $attr - The media item attributes.
 * @param object $attachment - The media item post object.
 * @return array
 */
function dd_custom_attribute_acf( $attr, $attachment ) {
    // get_field of whatever your field name is here.  
    $attr['data-attr'] = get_field( 'tag_cat_acf', $attachment->ID );
    return $attr;
}

我将它添加到我的functions.php中,它确实将我的自定义字段的内容添加到了图像html中。

【讨论】:

  • 非常感谢霍华德。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2017-01-15
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 2012-01-04
  • 2021-10-06
  • 2021-06-06
相关资源
最近更新 更多