如果属性值为0,则显示/隐藏部分短代码

时间:2014-08-14 06:38:44

标签: wordpress shortcode

大家好我想隐藏一部分短代码,当像closebutton这样的属性值为0时。 这是我使用的代码

function atalertshortcode($atts, $content = null) {
extract(shortcode_atts(array(
    "class" => '',"text" => '',"size" => '',"closebutton" => '',"other" => ''
), $atts));
return '<div class="uk-alert '.$class.' '.$size.'" data-uk-alert="" '.$other.'><a href="" class="uk-alert-close uk-close"></a> '.$text.' </div>';}

我想在closebutton值为0时隐藏这段短代码

<a href="" class="uk-alert-close uk-close"></a>

请帮帮我

1 个答案:

答案 0 :(得分:0)

你走了:

function atalertshortcode($atts, $content = null) {
    extract(shortcode_atts(array(
        "class" => '',
        "text" => '',
        "size" => '',
        "closebutton" => '',
        "other" => '',
    ), $atts));

    $output = '<div class="uk-alert ' . $class . ' ' . $size . '" data-uk-alert="" ' . $other . '>';
    if ($closebutton) {
        $output .= '<a href="" class="uk-alert-close uk-close"></a>';
    }
    $output .= ' ' . $text . '</div>';

    return $output;
}

这基本上要求您将输出分成几个部分,并将可选部分包装在IF块中以检查所需条件(在这种情况下,我们检查$ closebutton是否以任何方式评估为0,并且如果没有,则链接被添加到短代码输出中。