doujieyu7062 2016-06-19 12:37 采纳率: 0%
浏览 118
已采纳

使用WordPress高级自定义字段时,“url”的非法字符串偏移量

I am converting a Bootstrap template into a WordPress theme.

For custom fields I am using the plugin Advanced Custom Fields. The issue is when I am trying to add an image with the help of Advanced Custom Fields, I get the following error:

Warning: Illegal string offset 'url' in C:\wamp64\www\my-site\wordpress\wp-content\themes\bootstraptowordpress\page-home.php on line 31

However when I am adding text through this plugin it shows no error.

I have no other plugins installed other than ACF.

Here is my code:

$home_page_logo = get_field('home_page_logo');

<div class="front_logo">

   <?php if( !empty($home_page_logo)): ?>

      <img src="<?php echo $home_page_logo['url']; ?>" alt="<?php  echo $home_page_logo['alt']; ?>" />

   <?php endif; ?>

 </div>
  • 写回答

2条回答 默认 最新

  • doufei8250 2016-06-19 23:35
    关注

    There are three ways that an image field can be returned in ACF (array, URL, or ID). It sounds like your field is set to return the URL - which returns as a string.

    Therefore, you need to access it like this:

    <?php echo $home_page_logo; ?>
    

    instead of this:

    <?php echo $home_page_logo['url']; ?>
    

    Alternatively, you can edit the set up for the field in your WordPress admin and configure it to return an image array instead of the URL:

    enter image description here

    If it's set to the array option, you can access the url like you are currently doing so, as well as access a range of other data pertinent to the image (such as its width and height, WordPress attachment ID, caption if entered, etc.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题