alibaba alibaba - 6 months ago 32
PHP Question

Advanced Custom Fields get sub field slide image

When i use this type of code

<?php echo get_sub_field('slide_image')['url']; ?>


then it's give me a


Parse error: syntax error, unexpected '[', expecting ',' or ';' in
/home/sites/xyz.com/public_html/wp-content/themes/abc/partials/banner-images.php
on line 9


this sort of error so what can i do in this situation. my total code is below

<?php if(have_rows('banner_slides')): // if have rows ?>

<div class="slideshow slideshow--banner" data-palm="palm-hidden">

<span id="prev" class="cycle-prev"><</span>

<?php while ( have_rows('banner_slides') ) : the_row(); // Loop through slides ?>

<div class="slideshow__slide" style="background-image: url(<?php echo get_sub_field('slide_image')['url']; ?>);">

Answer

It depends on what the return type of your sub-field is(you can set this while editing the sub-field). It will either by an array, an image url, or an image id.

If it's an array:

$img = get_sub_field('slide_image');
<div class="slideshow__slide" style="background-image: url(<?php echo $img['url']; ?>);">

If it's a url:

$img = get_sub_field('slide_image');
<div class="slideshow__slide" style="background-image: url(<?php echo $img; ?>);">

If it's an id:

$img = get_sub_field('slide_image');
<div class="slideshow__slide" style="background-image: url(<?php echo get_permalink($img); ?>);">