如果参数等于true,则在php函数内添加if子句仅显示html属性

时间:2013-04-29 07:16:45

标签: php wordpress

我正在创建一个wordpress按钮短代码,并希望传入3个参数 - 类,类型然后是真/假,具体取决于链接是否应该是外部的。但是目前我还不确定如何在函数中包含这个属性,并且想知道是否有人可以告诉我如何解决这个问题?

PHP

function button($atts, $content = null) {
   extract(shortcode_atts(array('link' => '#', 'type' => '', 'external' => 'false'), $atts));
   return '<a href="/'. $link .'" class="btn" ' . if( 'external' == 'true' ) . 'target="_blank"><i class="btn-'. $type .'"></i>' . do_shortcode($content) . '</a>';
}
add_shortcode('button', 'button');

1 个答案:

答案 0 :(得分:3)

保持简单。做你的操作,分配给变量&amp;使用它,如下所示。

$target = "_self";
if($external == 'true' ){
   $target = "_blank";
}
return '<a href="/'. $link .'" class="btn"  target="' . $target . '"><i class="btn-'. $type .'"></i>' . do_shortcode($content) . '</a>';