如何为此生产线编写三元运算符?

时间:2015-02-25 23:16:24

标签: php wordpress advanced-custom-fields

我无法弄清楚如何为这一行的结尾写一个三元运算符(即“位置”和“描述”)。我已经定义了$location$description变量,但我收到的错误是unexpected $location variable

基本上,对于位置和描述,我试图说:如果字段已填写,则显示该字段。如果没有,请不要打印任何内容。

$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';

以下是完整的背景信息:

if( have_rows('slides', $frontpage_id) ):
    while ( have_rows('slides', $frontpage_id) ) :
        the_row();
        $group = get_sub_field('group');
        $count = 1;
        $i = 0;

        $nav .= '<ul id="nav_'.$group.'" style="display: none;">';
        $content .= '<div class="image-data-container image-container-'. $group .'">';
        while( have_rows('images') ) : the_row(); $count++;
            $image_url = get_sub_field('image');
            $location = get_sub_field('location');
            $description = get_sub_field('description');
            $isize = @getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
            $content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
            $nav .= '<li ><a href="javascript:void(0)" class="change_gallery" rel="' . $i . '" data-group="' . $group . '">' . get_sub_field('title') . '</a></li>';
            $i++;
        endwhile;
        $content .= '</div>';
        $nav .= '</ul>';

    endwhile;
endif;
?>

1 个答案:

答案 0 :(得分:1)

其他事实是你的$ content变量是格式化的噩梦。三元运算符语法根据条件返回结果,它应与$ content属性

分开
$someVariable = (get_sub_field('location')) ? '<em>'.$location.'</em>' : '';

现在在$content变量中使用一些变量

以下是包含评论的代码:

while( have_rows('images') ) : the_row(); $count++;
  $image_url = get_sub_field('image');

  // you have already defined the location variable here 
  // It also means and I am assuming they your get_sub_field is returning a string
  // If you are receiving a boolean or null value than and than only than you can do your ternary condition call of
  // get_sub_field('location') ? '<em>'$location'</em>' : ''
  // and if you are receiving a string field then you will have to explicitly check the condition
  // something like this (get_sub_field('location') === '') ? '<em>'$location'</em>' : ''
  // I am not that familiar with wordpress framework or the ACF plugin but it looks like get_sub_field will return something so you actually wont need the ternary condition
  $location = get_sub_field('location');
  // Similar case with description
  $description = get_sub_field('description');
  $isize = @getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
  // After removing the ternary condition your content variable will look like this
  $content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . '<em>' . $location . '</em>' . '<hr><span class="details">Details [+]</span><div class="details-display">' . $description . '</div>' . '</div></div></div>';
  $nav .= '<li ><a href="javascript:void(0)" class="change_gallery" rel="' . $i . '" data-group="' . $group . '">' . get_sub_field('title') . '</a></li>';
  $i++;
endwhile;

你知道你总能让它更具可读性

// You know instead of all the concatenation you can do this instead
  $templateDirURI = get_template_directory_uri();
  $groupLower = strtolower($group);
  $title = get_sub_field('title');
  $content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong><hr><em>{$location}</em><hr><span class='details'>Details [+]</span><div class='details-display'>{$description}</div></div></div></div>";

更新代码

  $content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong>";
  // Assuming the user did not submit location information
  if($location !== "")
    $content .= "<hr><em>{$location}</em><hr>";
  if($description !== "")  
    $content .= "<span class='details'>Details [+]</span><div class='details-display'>{$description}</div>";
  $content .= "</div></div></div>";