doulan3436
2017-10-30 18:24
浏览 216
已采纳

如何使用高级自定义字段在Wordpress中将特色图像添加到类别

I am new to WP and ACF and I am trying to get each category to display a featured image using a custom field set up with ACF.

I have added a field group with the location rule to show the field group if taxonomy term is equal to categories. I have then added the <?php the_field( 'add_featured_image' ); ?> to single.php

The custom field is displaying in the category options, allowing me to select a featured-image, but it is not displaying when published.

I have also tried the various different ways of targeting the ID of a single category instead of all the categories as per the ACF docs such as

<?php the_field( 'add_featured_image', 'category_6' ); ?>

or

<?php the_field('add_featured_image', 6); ?>

but nothing is working.

The nearest I have got is when using

<?php the_field( 'add_featured_image', 'category_6' ); ?>

which outputs this on the screen:

This suggests something is happening but I can't figure out what is happening after plenty of trying

I have also tried the get_field() function to no avail. I am using FoundationPress as a parent theme.

Any help would be greatly appreciated.

图片转代码服务由CSDN问答提供 功能建议

我是WP和ACF的新手,我试图让每个类别使用自定义字段显示特色图像 设置ACF。

如果分类术语等于类别,我添加了一个包含位置规则的字段组来显示字段组。 然后我添加了&lt;?php the_field('add_featured_image'); ?&gt; single.php

自定义字段显示在类别选项中,允许我选择特色图片,但它 发布时没有显示。

我还尝试了各种不同的方法来定位单个类别的ID而不是所有类别,如ACF文档,如 \ n

 &lt;?php the_field('add_featured_image','category_6');  ?&gt; 
   
 
 

 &lt;?php the_field('add_featured_image',6);  ?&gt; 
   
 
 

但没有任何效果。

我所用的最近的是 &lt;?php the_field('add_featured_image','category_6'); ?&gt;

在屏幕上输出:

后弄清楚发生了什么

我还尝试了 get_field()功能无济于事。 我使用FoundationPress作为父主题。

我们将非常感谢任何帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqq46733 2017-10-30 19:27
    已采纳

    It looks to me like you have it already with <?php the_field( 'add_featured_image', 'category_6' ); ?>. It's just outputting an object from which you have to get the src. Use $url = $add_featured_image['url']; and then echo it out into your image <img src="<?php echo $url; ?>"/>. Same method then to get the alt attribute.

    You'll see more here.

    Hope that helps. Best of luck!

    点赞 打赏 评论

相关推荐 更多相似问题