Wp add_shortcode attribute не изменяет значение атрибута - PullRequest
0 голосов
/ 30 января 2020


Я создал шорткод с атрибутами (с помощью Google), но он не изменяет значение атрибутов. Я много гуглил эту проблему, но не нашел решения.
Вот мой код:

function infobox_shortcode( $atts ) {

    $array = shortcode_atts(
        array(
            'Background' => 'rgba(45, 90, 171, 0.1)',
            'Border' => 'rgba(45, 90, 171, 0.5)',
            'Color' => '#2561ea',
        ),
        $atts
    );
        return "<div class='note' style='background-color:{$array['Background']};border-color:{$array['Border']};color:{$array['Color']};'>
                <span class='note-icon'><i class='ti ti-info'></i></span>
                <span class='ml-2'>
                    Want to use this template in Shopify, Wordpress or other platform?
                </span>
            </div>";

}
add_shortcode( 'InfoBox', 'infobox_shortcode' );

И я называю его в индексе моей темы. php страница как это:

<?= do_shortcode('[InfoBox Background="red"]'); ?>

Но это не меняет значение атрибута Background, я не знаю, где я иду не так. Будет полезно, если вы, люди, поможете мне.

Ответы [ 4 ]

1 голос
/ 30 января 2020

Это прекрасно работает для меня.

function infobox_shortcode($atts, $content = null)
{

    $a = shortcode_atts(array(
        'background' => 'rgba(45, 90, 171, 0.1)',
        'border' => 'rgba(45, 90, 171, 0.5)',
        'color' => '#2561ea',
    ), $atts);

    $content = ($content) ? $content : 'Want to use this template in Shopify, Wordpress or other platform?';

    $output = '<div class="note" style="background-color: ' . $a['background'] . '; border: ' . $a['border'] . '; color: ' . $a['color'] . ';">';
    $output .= '<span class="note-icon"><i class="ti ti-info"></i></span>';
    $output .= '<span class="ml-2">';
    $output .= $content;
    $output .= '</span>';
    $output .= '</div>';

    return $output;

}

add_shortcode('InfoBox', 'infobox_shortcode');
1 голос
/ 30 января 2020
Атрибутом

будут маленькие буквы.

'background' => 'rgba(45, 90, 171, 0.1)',
'border' => 'rgba(45, 90, 171, 0.5)',
'color' => '#2561ea',
1 голос
/ 30 января 2020
Here, some changes in code. Try this one, may this will give you solution or idea about your issue..

<?php
function infobox_shortcode( $atts ) {

$array = shortcode_atts(
    array(
        'background' => 'rgba(45, 90, 171, 0.1)',
        'border' => 'rgba(45, 90, 171, 0.5)',
        'color' => '#2561ea',
    ),$atts);
print_r($atts); // Remove, when you are fine with your atts! 
return 'Hello, World: '.$atts['background'];  //Remove, when you are fine with your expected output! 

$html = '<div class="note" style="background-color:{"'.$atts['background'].'"};border-color:{"'.$atts['border'].'"};color:{"'.$atts['color'].'"};"><span class="note-icon"><i class="ti ti-info"></i></span><span class="ml-2">Want to use this template in Shopify, Wordpress or other platform?</span></div>';
return $html; 
}

add_shortcode( 'InfoBox', 'infobox_shortcode' );
?>
0 голосов
/ 30 января 2020

Это была моя ошибка, я создавал атрибуты с заглавной буквой, как это

'Background' => 'rgba(45, 90, 171, 0.1)',

Но это должно быть маленькими буквами, и теперь это сработало.

'background' => 'rgba(45, 90, 171, 0.1)',
...