我有以下代码循环遍历数组并构建我的 html 结构:
<?php foreach ($view->result as $id => $row): ?>
<?php
$data = $row->field_field_slider_image;
if (!count($data)) {
continue;
}
//dpm($row);
// add captions to an empty array
if (isset($row->field_data_field_slider_image_field_slider_image_title)) {
$captions["caption-fid-" . $row->field_data_field_slider_image_field_slider_image_fid] = '<div class="header"><h3>'
. l($row->node_title, $row->field_field_slider_link[0]['raw']['url']) . '</h3></div><div class="body">'
. drupal_render($row->field_body)
. '</div>'
. l(t('read more'), $row->field_field_slider_link[0]['raw']['url'], array('attributes' => array('class' => array('button_link'))));
}
$file = $data[0]['rendered'];
// assign iamge style
$style = isset($file['#image_style']) && !empty($file['#image_style']) ? $file['#image_style'] :
variable_get('glossy_style_ff_slideshow', 'front_featured_slideshow_image');
?>
<a href="<?php print url($row->field_field_slider_link[0]['raw']['url']); ?>"><img src="<?php print image_style_url($style, $file['#item']['uri']); ?>" alt="<?php print $row->field_data_field_slider_image_field_slider_image_alt; ?>" title="#caption-fid-<?php print $row->field_data_field_slider_image_field_slider_image_fid; ?>"/></a>
<?php endforeach;?>
问题是链接:$row->field_field_slider_link[0]['raw']['url']
不是必需的字段,因此并不总是存在。
如何首先检查该值是否已设置并打印替代行/如果没有则省略?
例如,如果没有链接值:
. l($row->node_title, $row->field_field_slider_link[0]['raw']['url']) . '</h3></div><div class="body">'
将会:
. print $row->node_title . '</h3></div><div class="body">'
和:
. l(t('read more'), $row->field_field_slider_link[0]['raw']['url'], array('attributes' => array('class' => array('button_link'))));
根本不会打印。
和:
<a href="<?php print url($row->field_field_slider_link[0]['raw']['url']); ?>"><img src="<?php print image_style_url($style, $file['#item']['uri']); ?>" alt="<?php print $row->field_data_field_slider_image_field_slider_image_alt; ?>" title="#caption-fid-<?php print $row->field_data_field_slider_image_field_slider_image_fid; ?>"/></a>
将会:
<img src="<?php print image_style_url($style, $file['#item']['uri']); ?>" alt="<?php print $row->field_data_field_slider_image_field_slider_image_alt; ?>" title="#caption-fid-<?php print $row->field_data_field_slider_image_field_slider_image_fid; ?>"/>
任何帮助将不胜感激。
谢谢