我正在将wordpress的简码添加到woocommerce电子邮件模板中。
do_shortcode('[sample_test name="additional_gift_msg"]);
然后我正在使用它来显示电子邮件中的值。我能够显示价值。
function th_email_shortcode_handler( $atts ) {
if ( ! empty( $atts['name'] ) ) {
$field = $atts['name'];
echo 'Found me';
}
}
add_shortcode('sample_test','th_email_shortcode_handler');
但是我需要在此处理函数中使用$order
或$order_id
才能从post meta中获取一些价值。如何使用这些变量?简码处理程序函数位于functions.php中。
我也尝试了以下操作,但$ order_id仍然为空。
do_shortcode('[sample_test name="additional_gift_msg" order_id=' . $order->get_id() . ']');
答案 0 :(得分:0)
下面的代码将解决问题。
function th_email_shortcode_handler( $atts ) {
if ( ! empty( $atts['name'] ) ) {
$field = $atts['name'];
echo 'Found me';
}
global $wp;
$order_id = absint( $wp->query_vars['order_id'] );
if ( empty($order_id) || $order_id == 0 )
return; // Exit;
return $order_id;
}
add_shortcode('sample_test','th_email_shortcode_handler');