Я работаю над созданием коротких кодов для своего блога. Я могу установить единственный параметр для моего короткого кода, но не знаю, как установить другой параметр.
Например, я могу использовать [myshortcode myvalue]
для вывода блока html в [myshortcode myvalue]
.
Вот что я сейчас использую:
function test_shortcodes( $atts ) { extract( shortcode_atts( array( 'myvalue' => '<div class="shortcodecontent"></div>' ), $atts ) ); return $myvalue; } add_shortcode( 'myshortcode', 'test_shortcodes' );
Теперь, как я могу использовать [myshortcode myothervalue]
для вывода другого блока html?
Обратите внимание, что короткий код тот же, только параметр изменен.
Давайте посмотрим на короткий код
[SH_TEST var1="somevalue" var2="someothervalue"]THE SHORTCODE CONTENT[/SH_TEST]
функция обработчика коротких сообщений принимает 3 параметра
$atts
– массив атрибутов, передаваемых в нашем случае в коротком коде:
$atts['var1']
имеет значение 'somevalue'
$atts['var2']
устанавливается как 'someothervalue'
$content
– это строка значения, заключенная в тегах THE SHORTCODE CONTENT
, в нашем случае: – $content
установлено в THE SHORTCODE CONTENT
$tag
– это строка тега shortcode, в нашем случае: – $tag
установлен в SH_TEST
Когда я создаю короткий код, я обычно определяю значения по умолчанию и объединяю их со значениями, представленными тегом shortcode ex:
add_shortcode('SH_TEST','SH_TEST_handler'); function SH_TEST_handler($atts = array(), $content = null, $tag){ shortcode_atts(array( 'var1' => 'default var1', 'var2' => false ), $atts); if ($atts['var2']) return 'myothervalue'; else return 'myvalue'; }
Если вы используете короткий код, такой как atts[0]
будет содержать значение:
add_shortcode( 'test', 'test_callback' ); function test_callback( $atts ) { return $atts[0]; }
Другой способ – вызвать значение с именем:
[myshortcode val="myvalue"] function test_callback( $atts ) { return $atts["val"]; }
Вам лучше делать это так:
function test_shortcodes( $atts ) { extract( shortcode_atts( array( 'type' => 'myvalue' ), $atts ) ); switch( $type ){ case 'myvalue': $output = '<div class="shortcodecontent"></div>'; break; case 'myothervalue': $output = '<div class="othershortcodecontent"></div>'; break; default: $output = '<div class="defaultshortcodecontent"></div>'; break; } return $output; } add_shortcode( 'myshortcode', 'test_shortcodes' );
Используйте его так:
[myshortcode type="myvalue"]
для вывода <div class="shortcodecontent"></div>
а также
[myshortcode type="myothervalue"]
для вывода <div class="othershortcodecontent"></div>
Этот способ работает для меня во всех случаях, используя [myshortcode type = "myvalue"]
function test_shortcodes( $atts = array() ) { extract( shortcode_atts( array( 'type' => 'myvalue' ), $atts ) ); switch( $atts['type] ){ case 'myvalue': $output = '<div class="shortcodecontent"></div>'; break; case 'myothervalue': $output = '<div class="othershortcodecontent"></div>'; break; default: $output = '<div class="defaultshortcodecontent"></div>'; break; } return $output; } add_shortcode( 'myshortcode', 'test_shortcodes' );
И если вы хотите добавить еще один параметр, все, что вам нужно сделать, это [myshortcode type = "myvalue" other = "somevalue"]
test_shortcodes( $atts = array() ) { extract( shortcode_atts( array( 'type' => 'myvalue', //Could be default 'other' => 'somevalue' //Could be default ), $atts ) ); switch( $atts['other'] ){ case 'somevalue': $output = '<div class="shortcodecontent">somevalue</div>'; break; case 'myothervalue': $output = '<div class="othershortcodecontent"></div>'; break; default: $output = '<div class="defaultshortcodecontent"></div>'; break; } return $output; } add_shortcode( 'myshortcode', 'test_shortcodes' );
надеюсь, это поможет