下面的代码应该允许包含许多属性的短代码,但并非所有属性都始终存在,在这种情况下,我不希望包含的元素显示任何内容。然而,无论是否有条件语句,当我上载函数文件时,我只会在前端和后端收到一个空白页。
以下是带条件语句的完整快捷码:
function info($atts, $content = null) {
extract(shortcode_atts(array(
"name" => \'\',
"image" => \'\',
"address" => \'\',
"phone" => \'\',
"email" => \'\',
"website" => \'\',
"description" => \'\',
"amenities" => \'\'
), $atts));
return \'<span class="sort">\'
if($image) { \'<img src="\'.$image.\'" />\'}\'
<span class="name">\'.$name.\'</span>
<span class="details">\'
if($phone) { $phone }
if($address) { \'• <a href="http://maps.google.com/?q=\'.$address.\'" target="_blank">\'.$address.\' <img src="<?php bloginfo(\'template_directory\'); ?>/_/images/mapmarker.png" width="16" height="16" alt="Map" /></a><br/>\'}
if($email) { \'<a class="infomail" href="mailto:\'.$email.\'">Send Email</a>\' }
if($website) { \'<a class="infosite" href="\'$website\'">Visit Website</a>\' }
if($description) { \'<p class="infodetails">\'.$description.\'</p>\' }
if($amenities) { \'<p class="amenities">\'.$amenities.\'</p>\' }
\'</span>
</span>\';
}
add_shortcode("info", "info");
谢谢你的建议!
最合适的回答,由SO网友:Michael 整理而成
您的条件中断了字符串的串联(无论如何,这不在您的代码中)
尝试重写此部分:
function info($atts, $content = null) {
extract(shortcode_atts(array(
"name" => \'\',
"image" => \'\',
"address" => \'\',
"phone" => \'\',
"email" => \'\',
"website" => \'\',
"description" => \'\',
"amenities" => \'\'
), $atts));
$output = \'<span class="sort">\';
if($image) { $output .= \'<img src="\'.$image.\'" />\';}
$output .= \'<span class="name">\'.$name.\'</span> <span class="details">\';
if($phone) { $output .= $phone; }
if($address) { $output .= \' • <a href="http://maps.google.com/?q=\'.$address.\'" target="_blank">\'.$address.\' <img src="\' . get_bloginfo(\'template_directory\') . \'/_/images/mapmarker.png" width="16" height="16" alt="Map" /></a><br/>\'; }
if($email) { $output .= \' <a class="infomail" href="mailto:\'.$email.\'">Send Email</a>\'; }
if($website) { $output .= \' <a class="infosite" href="\'.$website.\'">Visit Website</a>\'; }
if($description) { $output .= \'<p class="infodetails">\'.$description.\'</p>\'; }
if($amenities) { $output .= \'<p class="amenities">\'.$amenities.\'</p>\'; }
$output .= \'</span> </span>\';
return $output;
}
add_shortcode("info", "info");