shortcode_atts 不解析自定义值而是加载默认值
shortcode_atts not parsing custom values instead defaults loaded
我制作了我自己的短代码函数,当短代码的调用有效时,我的页面查询在 returns 结果中 - 它从不使用任何设置,而是默认设置,就好像 $att 为空一样。
function test_shortcode( $atts ) {
$filter = shortcode_atts(
array(
'type' => 'major',
'sort' => 'name',
'size' => 'large',
'links' => 'yes',
),
$atts,
'customshortcode'
);
echo 'ATTS:';
print_r($atts);
echo'FILTER';
print_r($filter);
//code to query posts removed
}
add_shortcode( 'customshortcode', 'test_shortcode' );
在 post 然后我可以添加..
[customshortcode type:"other" size:"small" sort:"rand" links:"no"]
查看结果
ATTS
Array
(
[0] => type:"other"
[1] => size:"small"
[2] => sort:"rand"
[3] => links:"no"
)
FILTER
Array
(
[type] => major
[sort] => name
[size] => large
[links] => yes
)
我可以看到函数中收到了 $atts 值,但是 $filter 没有更新。我希望两个数组在打印时是相同的。据我所知,我在这里遵循 coxed 格式 https://codex.wordpress.org/Function_Reference/shortcode_atts
您以错误的方式传递属性。
应该使用=
而不是:
。
请通过 https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ 了解更多关于带参数的简码。
试试 [customshortcode type="other" size="small" sort="rand" links="no"]
我制作了我自己的短代码函数,当短代码的调用有效时,我的页面查询在 returns 结果中 - 它从不使用任何设置,而是默认设置,就好像 $att 为空一样。
function test_shortcode( $atts ) {
$filter = shortcode_atts(
array(
'type' => 'major',
'sort' => 'name',
'size' => 'large',
'links' => 'yes',
),
$atts,
'customshortcode'
);
echo 'ATTS:';
print_r($atts);
echo'FILTER';
print_r($filter);
//code to query posts removed
}
add_shortcode( 'customshortcode', 'test_shortcode' );
在 post 然后我可以添加..
[customshortcode type:"other" size:"small" sort:"rand" links:"no"]
查看结果
ATTS
Array
(
[0] => type:"other"
[1] => size:"small"
[2] => sort:"rand"
[3] => links:"no"
)
FILTER
Array
(
[type] => major
[sort] => name
[size] => large
[links] => yes
)
我可以看到函数中收到了 $atts 值,但是 $filter 没有更新。我希望两个数组在打印时是相同的。据我所知,我在这里遵循 coxed 格式 https://codex.wordpress.org/Function_Reference/shortcode_atts
您以错误的方式传递属性。
应该使用=
而不是:
。
请通过 https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ 了解更多关于带参数的简码。
试试 [customshortcode type="other" size="small" sort="rand" links="no"]