0

我正在使用wordpress php 小部件,但我无法处理 Wordpress 简码 - php 只是呈现简码而不是处理它。这是我所做的

  1. 在我的活动主题 function.php 文件中添加了过滤器

    add_filter('widget_text', 'shortcode_unautop');
    add_filter('widget_text', 'do_shortcode');
    
    // Allow shortcodes in php code widget
    add_filter('widget_execphp', 'shortcode_unautop');
    add_filter('widget_execphp', 'do_shortcode');
    
  2. 将以下内容添加到 php 小部件

    <?php
    $id = get_the_ID();
    $amazon_product_asin_value = get_post_meta($id, 'amazon_product_asin', true);
      echo do_shortcode('<div> [amazon asin=' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default] </div>');
    ?>
    

我也试过没有 do_shortcode 和相同的结果。

2 正确输出的简码

[amazon asin=B008I20FT8&template=buynowamazon_widget&chan=default]

如果我只是在标准文本小部件中输入它就可以了

我正在使用生成简码的 Amazon Link 插件

有任何想法吗?

4

1 回答 1

0

The asin parameter value might be confusing the shortcode processor; try putting quotes around it.

echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default"] </div>');

Alternatively, does the shortcode accept each argument to that parameter as a separate parameter?

echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '" template="buynowamazon_widget" chan="default"] </div>');

Edit: Looking at the plugin's code, I don't think you need these lines:

add_filter('widget_execphp', 'shortcode_unautop');
add_filter('widget_execphp', 'do_shortcode');

They will try to process the shortcode, before the PHP is executed, and thus you won't get the shortcode correctly processed. Comment them out and see what the plugin does now.

于 2012-12-25T01:22:02.700 回答