需要知道您计划如何使用此快捷码,因为如果用户有多个订单,那么按照您的要求执行操作会引发您希望该快捷码执行什么操作的问题?
如果此短代码将显示在特定于特定顺序的页面或帖子上,则可以按如下方式创建短代码:
以下两个函数应位于主题函数中。php文件或在插件主php文件中。
这将注册您的短代码:
function register_my_shortcode() {
add_shortcode( \'get_current_order_number\', \'get_current_order_number_callback\' ) );
}
add_action( \'init\', \'register_my_shortcode\', 10 );
这是一个函数回调,它构建了您想要对短代码执行的操作:
function get_current_order_number_callback( $atts ) {
// This is where you can get your order number.
global $post;
$order = new WC_Order($post->ID);
$output = \'\';
// Good to do a check that the order number is not empty.
if ( ! empty( $order->get_order_number() ) ) :
// This is where you can declare attributes that you can add in your shortcode.
$atts = shortcode_atts( array(
\'id\' => \'default-id\',
\'container_class\' => \'default-container-class\',
\'class\' => \'default-class\',
), $atts );
// Now you can build the elements that you want to display the order number in.
$output .= \'<div class="\' . $atts[\'container_class\'] . \'">;
$output .= \'<span id="\' . $atts[\'id\'] . \'" class="\' . $atts[\'class\'] . \'">\' . $order->get_order_number() . \'</span>\';
$output .= \'</div>\';
// If order number is not empty will return your shortcode build above.
return $output;
endif;
// If order number is empty will return false.
return false;
}
您的短代码如下所示,如果没有ATT,将使用默认值:
[get_current_order_number]
或添加ATT:
[get_current_order_number id="43" container_class="custom-container" class="order-number-text"]