2017-05-08 8 views
0

このショートコードを複数回使用すると、すべてのショートコードがaddpadクラスとインラインスタイルの出力を取得します。どのようにすれば、属性を持つショートコードだけがコードを出力するのですか?Wordpress PHPショートコードページのすべてのショートコードを引き出す場合

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] 
+0

'one_third_last'のショートコードはありますか? – Chris

+0

はい、申し訳ありませんが、このコメントは表示されませんでした。私は最後のものをone_thirdに変更しただけで、同じ問題を抱えていました。 –

答えて

0

それが設定されていない場合、これらの行には、あなたが$bgのデフォルトを指定するために技術的には、isset($bg)はtrueを返します:

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 
} 
+0

この変更で、私はまだ同じ結果を見ています。 3つの "ブロック"すべてがまだクラスのaddpadを取得しています。 –

+0

@RobertL両方のif文を変更しましたか?彼らはまだ背景色/テキスト色のスタイルを追加していますか? – Chris

+0

はい、私はしました。赤いbgは1つだけに来ていますが、3つすべてがaddpadクラスです。 '関数block_one_third($ ATTS、= NULL $コンテンツ){ エキス(shortcode_atts(配列( 'BG' => ''、 'テキスト' => ''、 )、$ ATTS))。 ($ bg、 '')== 0 || strcmp($ text、 '')== 0){$ style = "style =" background-color: '。$ bg。'; color: ' $ strcmp($ text、 '')== 0){$ padit = 'addpad'; $ text; ''} ';} } 返信 '

' . do_shortcode($content) . '
'; } add_shortcode( 'one_third'、 'block_one_third'); ' –

関連する問題