在Woocommerce中使用自定义字段变量作为短代码参数值

在Woocommerce中,我使用 YITH WooCommerce条形码和QR码插件,我想使用自定义字段作为此短代码中参数的值。 这是相关文档 。</ p>

这就是我想要的(其中</ code> CUSTOMFILEDVALUE 是自定义字段的值)</ code>:</ p>

  [yith_render_barcode value =“CUSTOMFILEDVALUE”protocol =“CODE39”] 
</ code> </ pre>

是否可以在此类型中包含自定义字段值 短代码? 如何做到这一点?</ p>

任何帮助将不胜感激。</ p>
</ div>

展开原文

原文

In Woocommerce, I am using YITH WooCommerce Barcodes and QR Codes plugin and I would like to use a custom field as a value for an argument in this shortcode. Here is the related documentation.

That is what I would like (whereCUSTOMFILEDVALUEis the value of the custom field):

[yith_render_barcode value="CUSTOMFILEDVALUE" protocol="CODE39"]

Is it possible to include a custom field value in this kind of short code? How this can be done?

Any help will be appreciated.

dongpu4141
dongpu4141 在HTMLEDITORINSIDEAPLUGIN中
2 年多之前 回复
drnxnbf199049
drnxnbf199049 这是在后端内容编辑器中还是在模板中?
2 年多之前 回复

1个回答

To do that you can embed an existing Shortcode inside a custom Shortcode function. The following code is an example based on other similar functional answers.

In this code I get the post id of the pages, posts or custom posts. You can specify a post ID using the shortcode id argument as in the original shortcode.

The code:

function custom_yith_render_barcode( $atts ) {
    // Shortcode attributes
    $atts = shortcode_atts( array(
        'id' => '0', // Product ID
        'hide_if_empty' => '1',
        'value' => '',
        'protocol' => 'EAN8',
    ), $atts, 'render_barcode' );

    global $post;

    if( '0' === $atts['id'] && $post && is_object($post) )
        $id = $post->ID;
    elseif( $atts['id'] > 0 )
        $id = $atts['id'];

    $hide     = $atts['hide_if_empty'];
    $value    = get_post_meta( $id, $atts['value'], true ) ? get_post_meta( $id, $atts['value'], true ) : $atts['value'];
    $protocol = $atts['protocol'];

    return do_shortcode( "[yith_render_barcode id='$id' hide_if_empty='$hide' value='$value' protocol='$protocol']" );
}
add_shortcode('render_barcode', 'custom_yith_render_barcode');

Code goes in function.php file of your active child theme (or active theme). It should works.


USAGE -

Below the meta_key has to be replaced with the meta key of your custom field. All other YITH Shortcode arguments are unchanged and available. Only value argument is used to pass the custom field meta key, allowing to get the custom field value in embedded YITH shortcode.

1) In the Wordpress page or post editor:

[render_barcode value="meta_key" protocol="CODE39"]

2) In PHP code:

echo do_shortcode( "[render_barcode value='meta_key' protocol='CODE39']" );
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问