• 03-05-2019, 05:06:49
    #1
    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' );
  • 03-05-2019, 05:37:41
    #2
    Eğer doğru anladıysam; bu kısmı güncelleyin. Değer yoksa style="" 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> ';
  • 03-05-2019, 16:02:19
    #3
    MOmerAlpi adlı üyeden alıntı: mesajı görüntüle
    Eğer doğru anladıysam; bu kısmı güncelleyin. Değer yoksa style="" 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> ';
    Evet doğru anlamışsınız değer yoksa görünmemesi lazım ancak bu şekilde düzenlediğim de görünmeye devam ediyor.
  • 03-05-2019, 16:03:31
    #4
    SkyGhostAir adlı üyeden alıntı: mesajı görüntüle
    Evet doğru anlamışsınız değer yoksa görünmemesi lazım ancak bu şekilde düzenlediğim de görünmeye devam ediyor.
    Team varsa özelden bilgileri gönderebilirsiniz. Çünkü testini yapmıştım; sıkıntı yoktu.
  • 04-05-2019, 03:57:37
    #5
    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' );
  • 04-05-2019, 04:32:25
    #6
    Üyeliği durduruldu
    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..