SkyGhostAir adlı üyeden alıntı: mesajı görüntüle
Merhabalar,

Şöyle bir shortcode yazdım ancak sytle="" kısmı içerik olsa da olmasa da görünüyor. Bunu nasıl üç değer de shortcode içerisinde yoksa görünmeyecek hale getirebilirim? Boş görünmesini istemiyorum.
function dm_button( $atts, $content = null ) {
extract(shortcode_atts(
array(
"color" => '',
"colorb" => '',
),
$atts));
if($color) $colors="border: 2px solid ".$color.";";
if(!$color) $colors="";
if($colorb) $colorbs="background-color: ".$colorb.";";
if(!$colorb) $colorbs="";
if($float=="right") $floats="float: right; width: auto;";
if($float=="left") $floats="float: left; width: auto;";
if($float=="middle") $middle="style="display: table; margin-right: auto; margin-left: auto;" ";
if($float=="center") $middle="style="display: table; margin-right: auto; margin-left: auto;" ";
if(!$float) $floats="";
return '
<div class="dm-button-external"><a href="#"><button style="'.$colors.' '.$colorbs.' '.$floats.'" class="bt-button">'.$content.'</button></a></div>
';
}
add_shortcode( 'button', 'dm_button' );
Kodlarınızda hatalar var.

MOmerAlpi adlı üyeden alıntı: mesajı görüntüle
Eğer doğru anladıysam; bu kısmı güncelleyin. Değer yoksa gözükmez..
return ' <div class="dm-button-external"><a href="#"><button ' . ((isset($colors) || isset($colorbs) || isset($floats)) ? 'style="'.$colors.' '.$colorbs.' '.$floats.'"': null) . ' class="bt-button">' . $content.'</button></a></div> ';
Normalde bu arkadaşın kodu çalışması gerekirdi.

trgino adlı üyeden alıntı: mesajı görüntüle
function dm_button( $atts, $content = null ) {
extract(shortcode_atts(
array(
"color" => '',
"colorb" => '',
), $atts));
$stylez = array();
if($color) $stylez[]='border: 2px solid '.$color.';';
if($colorb) $stylez[]='background-color: '.$colorb.';';
if($float=="right") $stylez[]=='float: right; width: auto;';
if($float=="left") $stylez[]='float: left; width: auto;';
if($float=="middle") $stylez[]='display: table; margin-right: auto; margin-left: auto;';
if($float=="center") $stylez[]'display: table; margin-right: auto; margin-left: auto;';
return '
<div class="dm-button-external"><a href="#"><button '.(!empty($stylez) ? 'style="'.implode(' ',$stylez).'"' : '').' class="bt-button">'.$content.'</button></a></div>
';
}
add_shortcode( 'button', 'dm_button' );
Bu arkadaş hataları düzeltmiş ve mantıklı olarak değişkenler yerine dizi oluşturarak kodunuza can vermiş.

Ek olarak if lerin yerine switch kullansanız bence daha iyi olurdu.

    switch($float) {
        case 'right':
            $stylez[]=='float: right; width: auto;';
        break;
        
        case 'left':
            $stylez[]='float: left; width: auto;';
        break;
        
        case 'middle':
            $stylez[]='display: table; margin-right: auto; margin-left: auto;';
        break;
        
        case 'center':
            $stylez[]'display: table; margin-right: auto; margin-left: auto;';
        break;
    }
gibi..