2013-04-29 26 views
0

我創建了一個wordpress按鈕短代碼,並且想要傳入3個參數 - class,type和true/false,具體取決於鏈接是否應該是外部的還是不是。但是現在我不確定如何在函數內包裝這個屬性,並且想知道是否有人可以告訴我如何解決這個問題?如果參數等於true,則在php函數中添加一個if子句以僅顯示html屬性

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'); 

回答

3

保持簡單。做你的操作,分配給變量&使用它,如下。

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

PHP的鬆散打字是某種痛苦。爲了保持你的代碼清潔,我會推薦某事。就像if($ external === true) – jossif 2013-04-29 08:11:44

+0

@jossif - OP將變量作爲字符串'「true」傳遞給'',你的條件將會失敗。 – Rikesh 2013-04-29 08:37:16

+0

當然,你是對的。我的建議暗示,意味着布爾值的值必須作爲布爾值來傳遞。 – jossif 2013-04-29 09:29:45

相關問題