Wordpress PHP短代码,如果设置了,则为页面上的所有短代码进行拉取

Wordpress PHP短代码,如果设置了,则为页面上的所有短代码进行拉取,php,wordpress,shortcode,isset,Php,Wordpress,Shortcode,Isset,在页面上多次使用此短代码时,每个短代码都会获得addpad类和内联样式的输出。如何做到只有具有属性的短代码才会输出代码 function block_one_third( $atts, $content = null ) { extract(shortcode_atts(array( 'bg' => '', 'text' => '', ), $atts)); if (isset($bg) || isset($text)){ $style =

在页面上多次使用此短代码时,每个短代码都会获得addpad类和内联样式的输出。如何做到只有具有属性的短代码才会输出代码

function block_one_third( $atts, $content = null ) {
   extract(shortcode_atts(array(
      'bg' => '',
      'text' => '',
   ), $atts));
   if (isset($bg) || isset($text)){ $style = ' style="background-color:'. $bg .';color:'. $text .'"';}
   if (isset($bg) || isset($text)) { $padit = ' addpad'; }
   return '<div class="one_third'. $padit .'"'. $style .'>' . do_shortcode($content) . '</div>';
}
add_shortcode('one_third', 'block_one_third');

[one_third]
Block 1 - Should have NO additional class or inline style
[/one_third]
[one_third bg="red"]
Block 2 WITH additional html output
[/one_third]
[one_third_last]
Block 3 - Should have NO additional class or inline style
[/one_third_last]
功能块三分之一($atts,$content=null){
提取(短码)附件(数组)(
“bg'=>”,
'文本'=>'',
)美元(附件);;
if(isset($bg)| isset($text)){$style='style=“background color:”.$bg.;color:“.$text.”;}
if(isset($bg)| isset($text)){$padit='addpad';}
返回“”。do_短代码($content)。“”;
}
添加快捷码(“三分之一”、“块三分之一”);
[三分之一]
块1-不应具有其他类或内联样式
[三分之一]
[三分之一bg=“红色”]
具有附加html输出的块2
[三分之一]
[最后三分之一]
块3-不应有其他类或内联样式
[/最后三分之一]
从技术上讲,
isset($bg)
将返回true,因为在这些行中,如果未设置,
$bg
的默认值:

extract(shortcode_atts(array(
  'bg' => '', // $bg default is '' if it wasn't found
  'text' => '', // $text default is '' if it wasn't found
), $atts));
与其检查它是否已设置,不如检查它是否等于默认值:

if (strcmp($bg,'') == 0 || strcmp($text,'') == 0 ) {
    // Do Something
}

你有最后三分之一的短码吗?是的,很抱歉我没有看到这个评论。我只是把最后一个改成了三分之一,但仍然有同样的问题。有了这个改变,我仍然看到了同样的结果。所有三个“块”仍在获取类addpad。@RobertL您是否更改了这两个if语句?他们是否仍然添加了背景色/文本颜色样式?是的,我添加了。红色背景仅在一个上通过,但所有三个都具有addpad类<代码>功能块三分之一($atts,$content=null){extract(短代码){atts(数组('bg'=>'','text'=>'',$atts));if(strcmp($bg',)==0 | | strcmp($text',)==0{$style='style='style='background color:'.$bg.';color:'.$text.'''.'''.'.'''.}如果(strcmp($bg,,)==0 | strcmp($text,)==0){$paddpadd返回“”。do_shortcode($content)。“”;}添加_shortcode('one_third','block_one_third')