2016-06-19 12:37



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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doufei8250 doufei8250 5年前

    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.)

    点赞 评论 复制链接分享
  • dry69034 dry69034 5年前

    It's better to use gravity forms plugin. You can to download here: Rocket Genius Gravity Forms WordPress Plugin

    But if want to fix your code, try to test if is array:

    $yes = array('this', 'is', 'an array');
    echo is_array($yes) ? 'Array' : 'not an Array';
    echo "
    $no = 'this is a string';
    echo is_array($no) ? 'Array' : 'not an Array';

    点赞 评论 复制链接分享