【问题标题】:Auto text woocommerce_short_description hook自动文本 woocommerce_short_description 挂钩
【发布时间】:2016-10-30 14:56:51
【问题描述】:

我正在尝试在 WooCommerce 文章的描述中创建一个自动文本,并将“文章仅在商店中可用”。

我想过把它放在这样的函数中:

add_filter ('woocommerce_short_description', 'in_single_product', 10, 2);

function in_single_product () {
    echo '<p> my-text-here </ p>';
}

但是文字没有出现在前端。仅当我将文本放在后端时才会出现。

有人知道为什么会这样吗?

【问题讨论】:

    标签: php wordpress woocommerce conditional product


    【解决方案1】:

    对单个产品页面使用条件并返回函数变量而不是回显它。

    这将替换单个产品页面中的帖子摘录:

    add_filter( 'woocommerce_short_description', 'single_product_short_description', 10, 1 );
    function single_product_short_description( $post_excerpt ){
        global $product;
    
        // compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        if ( is_single( $product_id ) )
            $post_excerpt = '<p class="some-class">' . __( "my-text-here", "your_theme_slug" ) . '</ p>';
    
        return $post_excerpt;
    }
    

    最好在 gettex 中为您的文本做广告以进行翻译(将 "your_theme_slug" 替换为您主题的 slug...

    参考:How to use different short description in shop page and in product page in woocommerce

    【讨论】:

    • 非常感谢您的回答!我尝试了代码,它运行良好,尽管在这种情况下,我曾经替换了简短描述中的文本。如果没有产品的简短描述,则文本不会出现。是否可以在没有产品简短描述的情况下显示代码文本?非常感谢
    • 谢谢,对不起!这是我的新问题的链接:stackoverflow.com/questions/38098914/…我希望做得好!
    【解决方案2】:

    正确的add_filter 语法

    // Define the my_editor_content  
    function my_editor_content( $post_excerpt )   {  
        // make filter magic happen here...
        return $post_excerpt;
    };
    // add the filter
    add_filter( 'woocommerce_short_description','my_editor_content',10, 1 );
    

    好吧,您可以在您的子主题的 functions.php 中使用此代码。

    function my_editor_content( $post_excerpt ) {
    $content = 'The default text, imposed by me, needs to be here in every product.';
    return $content.'<br>'.$post_excerpt;
    }
    
    add_filter('woocommerce_short_description', 'my_editor_content', 10, 1);
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2021-12-07
      相关资源
      最近更新 更多