我真的很难使用 WooCommerce PHP。这次我只是想在继续添加自定义元框之前查看产品帖子是否有标签。代码写入子主题的functions.php。
global $wp_query;
$id = $wp_query->post->ID;
if ( ! has_tag( "hasparts", $id )) {
die; // used return also
} else {
// add metabox
}
我收到以下错误
警告:尝试在第 14 行上的 C:\xampp\htdocs\wiz_invent\wp-content hemes\storefront-child unctions.php
中读取 null 属性“ID”我似乎无法理解如何查找基本帖子属性(ID、SKU)或如何以及在何处使用 $post 变量。
WooCommerce 标签是自定义分类法,因此它将与 WordPress has_term() 和 WooCommerce 标签的
product_tag
分类法一起使用。
以下是根据您的要求的完整示例:
// Add a metabox to WooCommerce Product conditionally
add_action( 'add_meta_boxes', 'admin_product_custom_metabox' );
function admin_product_custom_metabox() {
if( has_term( array('hasparts'), 'product_tag') ) {
add_meta_box(
'custom',
__('Custom Meta Box'),
'custom_metabox_content_callback',
'product',
'normal',
'high'
);
}
}
// Metabox content callback function
function custom_metabox_content_callback() {
echo 'Here goes the metabox content…';
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。