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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?