dseslyh6662605
2016-05-06 21:42
浏览 78
已采纳

高级自定义字段获取子字段幻灯片图像

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']; ?>);">
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dosin84644 2016-05-06 21:58
    已采纳

    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); ?>);">
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duannai5858 2016-05-06 23:33

    Object:

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

    URL:

    <?php if( get_field('slide_image') ): ?>        
          <div class="slideshow__slide" style="background-image: url(<?php the_field('slide_image'); ?>);">
    <?php endif; ?>
    

    ID:

    <?php 
    
    $img = get_field('slide_image');
    
    $size = 'full';
    
    if( $img ) {
    
        <div class="slideshow__slide" style="background-image: url(<?php echo wp_get_attachment_image( $img, $size ); ?>);">
    
    }
    
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题