【问题标题】:wordpress woocommerce shortcodewordpress woocommerce 简码
【发布时间】:2020-02-10 14:12:10
【问题描述】:

当我们请求这个简码时,我从这个函数中得到一个错误

function salex_func( $atts ){
   global $product;
if($product->is_on_sale()){
        echo '<span class="onsale soldout">';
    echo __( 'SALE!!!!', 'hello');
    echo '</span>';
}   
}
add_shortcode('saletex', 'salex_func');

【问题讨论】:

  • 你能把错误发给我吗?
  • 我不确定你的代码,因为我现在不在电脑上,但对于短代码的 wp 一般提示,我看到你正在回显输出的问题,这将使它在顶部显示内容而不是你把简码放在哪里。为此,我建议在第一次回显之前使用 ob_start();然后在最后一个回显行之后说: return ob_get_clean();这样,它将从顶部选择所有缓冲区输出并返回到简码函数,并将其显示在正确的位置。

标签: wordpress woocommerce shortcode


【解决方案1】:

您不能回显短代码输出。你必须退货。

function salex_func( $atts ){
   global $product;
    if($product->is_on_sale()){
        $output = '<span class="onsale soldout">';
        $output .= __( 'SALE!!!!', 'hello');
        $output .= '</span>';
     }
    return $output;   
}
add_shortcode('saletex', 'salex_func');

【讨论】:

    【解决方案2】:

    您不需要在短代码函数中回显。

    你可以试试这个代码:

    function salex_func( $atts ){
        global $product;
        if($product->is_on_sale()){ 
            ob_start(); ?>
            <span class="onsale soldout"><?php __( 'SALE!!!!', 'hello'); ?></span>
            <?php return ob_get_clean();
        }   
    }
    add_shortcode('saletex', 'salex_func');
    

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 2017-05-13
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多