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

如何使用高级自定义字段在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.

  • 写回答

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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害