dqd82461 2018-06-19 22:22 采纳率: 0%
浏览 141

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

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.

  • 写回答

1条回答

  • douqu8828 2018-06-19 23:57
    关注

    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']" );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退