拆分快捷代码属性不起作用

时间:2018-07-12 作者:dasgelbevomei

我想根据我在短代码中定义的属性调整比较。例如,我使用以下简短代码:

[comparison phonePrefix="0581",phoneFlatOption="true", 
accessModeDsl="true", accessModeCable="false", accessModeLte="false", 
accessModeSat="false"] 
下面是我的shortcode函数:

function comparison($atts){

$atts = shortcode_atts(
array(
        \'phonePrefix\' => \'030\',
        \'customerType\' => \'existingCustomer\',
        \'remainingContractDuration\' => \'6\',
        \'phoneFlatOption\' => \'true\',
        \'minSpeed\' => \'50000\',
        \'accessModeDsl\' => \'true\',
        \'onlyProductsWithoutTrafficLimitation\' => \'true\',
        \'accessModeCable\' => \'true\',
        \'accessModeLte\' => \'true\',
        \'accessModeSat\' => \'true\',
        \'contractDuration\' => \'-1\',
        \'mergeDefaults\' => \'false\',
    ), $atts, \'dslVergleich\');
 }
据我所知shortcode_atts() 函数,现在应替换默认值\'phonePrefix\' 具有0581. 但在我使用该函数后,仍然有默认值030.

这是一个var_dump 在我使用之前shortcode_atts():

array(5) { 
  [0]=> string(42) "phonePrefix="0581",phoneFlatOption="true"," 
  [1]=> string(21) "accessModeDsl="true","
  [2]=> string(24) "accessModeCable="false"," 
  [3]=> string(22) "accessModeLte="false"," 
  ["accessmodesat"]=> string(5) "false" 
} 
这是在我使用shortcode_atts():

array(12) {
  ["phonePrefix"]=> string(3) "030" 
  ["customerType"]=> string(16) "existingCustomer"     
  ["remainingContractDuration"]=> string(1) "6" 
  ["phoneFlatOption"]=> string(4) "true"
  ["minSpeed"]=> string(5) "50000" 
  ["accessModeDsl"]=> string(4) "true" 
  ["onlyProductsWithoutTrafficLimitation"]=> string(4) "true" 
  ["accessModeCable"]=> string(4) "true" 
  ["accessModeLte"]=> string(4) "true" 
  ["accessModeSat"]=> string(4) "true" 
  ["contractDuration"]=> string(2) "-1" 
  ["mergeDefaults"]=> string(5) "false"
}
谢谢你的帮助!

1 个回复
SO网友:Jacob Peattie

这个var_dump() 使用前shortcode_atts() 揭示了问题所在。让我们以更可读的格式查看该输出:

array(5) { 
    [0] => string(42) "phonePrefix="0581",phoneFlatOption="true"," 
    [1]=> string(21) "accessModeDsl="true"," 
    [2]=> string(24) "accessModeCable="false"," 
    [3]=> string(22) "accessModeLte="false"," 
    ["accessmodesat"]=> string(5) "false"
} 
如您所见,未正确解析属性。而不是\'attribute\' => \'value\' 您将得到一个数字索引的短码碎片数组。

这是因为您编写短码的方式,而不是短码代码本身。

[comparison phonePrefix="0581",phoneFlatOption="true", 
accessModeDsl="true", accessModeCable="false", accessModeLte="false", 
accessModeSat="false"] 
The lack of spaces between the attributes and the use of commas is incorrect formatting for shortcodes. 其格式应如下所示:

[comparison phonePrefix="0581" phoneFlatOption="true" 
accessModeDsl="true" accessModeCable="false" accessModeLte="false" 
accessModeSat="false"]

结束

相关推荐

Shortcode with foreach

我不能对foreach使用shortcode。它给了我语法错误。我错在哪里?function create_galeri_shortcode($atts) { $atts = shortcode_atts( array( ), $atts, \'galeri\' ); if ( has_post_format( \'gallery\' )) { $images = get_post_