【问题标题】:PHP using a variable inside do_shortcode() - Wordpress Page TemplatePHP 在 do_shortcode() 中使用变量 - Wordpress 页面模板
【发布时间】:2015-07-30 22:20:42
【问题描述】:

我想要的简码格式是:[product id="id#"] do_shortcode 的格式为:do_shortcode('[shortcode');

我的问题是如何插入 PHP 变量作为 id#?

我有一个变量 $itemid,我想像这样使用它:

echo do_shortcode('[product id="$itemid"])

但我一辈子都无法让它发挥作用。任何建议将不胜感激。

【问题讨论】:

  • 也许echo do_shortcode("[product id='.$itemid.']") ?
  • 你能分享你的add_shortcode 函数product 吗?

标签: php wordpress shortcode


【解决方案1】:

试试这个

$short = '[product id="'.$itemid.'"]';
echo do_shortcode($short);  

【讨论】:

    【解决方案2】:

    使用add_shortcode 函数创建您的自定义shortcode

    function products($id) {
        print_r($id);
    }
    add_shortcode( 'product', 'products' );
    
    
    $itemid = "132";
    

    连接$itemid 中的do_shortcode 函数如下所示

    echo do_shortcode('[product id="'.$itemid.'"]');
    

    输出

    Array ( [id] => 132 )
    

    【讨论】:

      【解决方案3】:

      这是单引号与双引号的问题,

      What is the difference between single-quoted and double-quoted strings in PHP?

      这个修复应该可以工作:

      echo do_shortcode("[product id=\"$itemid\"]")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-27
        • 2021-06-23
        • 1970-01-01
        • 2014-05-25
        • 2015-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多