短码可以包含条件语句吗?即使没有它们,我的快捷代码也会呈现空白页面

时间:2011-06-13 作者:5t3ph

下面的代码应该允许包含许多属性的短代码,但并非所有属性都始终存在,在这种情况下,我不希望包含的元素显示任何内容。然而,无论是否有条件语句,当我上载函数文件时,我只会在前端和后端收到一个空白页。

以下是带条件语句的完整快捷码:

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) { \'&bull; <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");
谢谢你的建议!

1 个回复
最合适的回答,由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 .= \' &bull; <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");

结束

相关推荐

write in functions.php

我只想在我的函数中添加此代码。php,以便在我的帖子结束后直接显示。目前,我正在将此代码添加到我的单篇文章中。php,但我想在函数中添加它。php,此代码用于获取各个帐户的所有推文,代码如下<?php function parse_twitter_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) { $feed = str_replace(\"&lt;\", \"<\", $feed);