最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Shortcode Always Displays Default Attribute Value

programmeradmin3浏览0评论

My shortcode always uses the default value for the single attribute, reviewstoshow, instead of the incoming attribute value. The incoming value has the correct number (per the second half of the dump included below) but the $numberofreviewstoshow var is always set to the default value (6).

Here is the shortcode being used:

[tdp_random_reviews reviewstoshow="8"]

This is the shortcode code:

add_shortcode( 'tdp_random_reviews', 'show_tdp_random_reviews' );

function show_tdp_random_reviews($atts) {
    $a = shortcode_atts( 
            array(
                'reviewstoshow' => '6'
            ), 
            $atts 
    );

var_dump($a);
var_dump($atts);

    $numberofreviewstoshow = $a['reviewstoshow'];

<snip>

}

Here is the output from the two var_dump() lines:

array(1) { ["reviewstoshow"]=> string(1) "6" }
array(1) { [0]=> string(30) "reviewstoshow="8"" }

Can you tell me where my mistake is? Thanks!

My shortcode always uses the default value for the single attribute, reviewstoshow, instead of the incoming attribute value. The incoming value has the correct number (per the second half of the dump included below) but the $numberofreviewstoshow var is always set to the default value (6).

Here is the shortcode being used:

[tdp_random_reviews reviewstoshow="8"]

This is the shortcode code:

add_shortcode( 'tdp_random_reviews', 'show_tdp_random_reviews' );

function show_tdp_random_reviews($atts) {
    $a = shortcode_atts( 
            array(
                'reviewstoshow' => '6'
            ), 
            $atts 
    );

var_dump($a);
var_dump($atts);

    $numberofreviewstoshow = $a['reviewstoshow'];

<snip>

}

Here is the output from the two var_dump() lines:

array(1) { ["reviewstoshow"]=> string(1) "6" }
array(1) { [0]=> string(30) "reviewstoshow="8"" }

Can you tell me where my mistake is? Thanks!

Share Improve this question asked Apr 30, 2020 at 18:46 BCarnathanBCarnathan 211 bronze badge 5
  • Have you tried a var_dump() of $numberofreviewstoshow? It looks right - $a['attributename']. You might also want to double-check types (string vs. integer) and quotes (smart vs. plain) if that's not giving you the value you expect. – WebElaine Commented Apr 30, 2020 at 18:56
  • Just tried that again to confirm my memory: var_dump($numberofreviewstoshow); Returns: string(1) "6" I replaced all of the quotes to ensure they are dumb. "It looks right" — thanks for making me feel less dumb! Have any more ideas? – BCarnathan Commented Apr 30, 2020 at 20:33
  • That second var_dump should be outputting an array, not a string. Are you sure your shortcode itself is correct? You're not using weird quotes around the 8, or something? Because WP is not recognizing the parameter. – vancoder Commented Apr 30, 2020 at 22:08
  • You solved the problem, vancoder. I replaced all quotes in the code per WebElaine but did not change out the quotes in the call. I don't know how wonky quotes got in there in the first place but thanks for the help both of you!!! – BCarnathan Commented Apr 30, 2020 at 22:38
  • One other things to mention is that you can't use camelCase names for attribute values. So fooBar is not allowed, foobar is. – NME New Media Entertainment Commented Feb 4, 2023 at 22:02
Add a comment  | 

1 Answer 1

Reset to default 2

The quotes in the shortcode call were not standard (despite them looking so). Replacing the old double quote characters with typed-into-notepad, copied, and pasted new quotes in the theme editor resolved the problems.

Thanks to @WebElaine and @vancoder for the help!

发布评论

评论列表(0)

  1. 暂无评论