首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您能从模板部件中生成WooCommerce短代码吗?

您能从模板部件中生成WooCommerce短代码吗?
EN

Stack Overflow用户
提问于 2017-05-16 01:29:10
回答 2查看 1.4K关注 0票数 0

我对创建基本填充WooCommerce签出模板部分的短代码感兴趣。例如,在我的子主题的functions.php中:

代码语言:javascript
复制
function shortcode_review_order() {
    //get the template part from woocommerce/templates/checkout/review-order.php
    wc_get_template_part('checkout/review-order');
}
add_shortcode( 'custom_review_order', 'shortcode_review_order' );

...and然后在我的页面..。

代码语言:javascript
复制
<div>[custom_review_order]</div>

当我尝试这个的时候,我的结账页面上什么也没有出现。

这有可能吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-16 01:42:44

你的代码中很少有错误..。

首先,您应该使用init钩子添加一个短代码。

代码语言:javascript
复制
add_action( 'init', 'add_shortcodes' );
function add_shortcodes(){
    add_shortcode( 'custom_review_order', 'shortcode_review_order' );
}

然后,缺少模板的.php部分。此外,它还需要如下所示的数组参数。使用wc_get_template可以得到更精确的结果。

代码语言:javascript
复制
function shortcode_review_order(){
    wc_get_template( 'checkout/review-order.php', array( 'checkout' => WC()->checkout() ) );
}

要更多地了解如何正确使用它的模板,请搜索插件上的每个模板。你会看到它是如何被使用的。你可以得到一个提示,你可以如何使用它为自己。

票数 0
EN

Stack Overflow用户

发布于 2019-08-02 07:53:00

我发现wc_get_template会回显模板,因为返回模板的短代码更好。您可以使用:

$string = wc_get_template_html( $template_name, $args, $template_path, $default_path );

它是“类似于wc_get_template,但返回HTML而不是输出。”

html.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43991151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档