获取shortcode
并获取自定义字段值:
function product_func($atts) {
$post_id = $atts[\'code\'];
$key = "my_custom_field_key";//for 1 field, you can do this 6 times for the 6 values
$custom_value = get_post_meta($post_id, $key, true);
return $custom_value;
}
add_shortcode(\'product\', \'product_func\');
如果要调试后元字段值,请使用以下代码:
function product_func($atts) {
$post_id = $atts[\'code\'];
//lets check if we are getting the att
echo "<h1>THE ATT, SHOULD MATCH THE CODE YOU SEND</h1></br>";
echo "<pre>";
var_dump($post_id);
echo "</pre>";
//lets check if we are getting the att
echo "</br><h1>WE MAKE SURE THE POST IS NOT NULL, MEANING IT SHOULD EXIST</h1></br>";
$post = get_post( $post_id );
echo "<pre>";
var_dump($post);
echo "</pre>";
//lets check the meta values for the post
echo "</br><h1>WE LIST ALL META VALUES TO CHECK THE KEY NAME OF THE CUSTOM FIELD WE WANT TO GET</h1></br>";
$meta = get_post_meta( $post_id );
echo "<pre>";
var_dump($meta);
echo "</pre>";
$key = "my_custom_field_key";//for 1 field, you can do this 6 times for the 6 values
$custom_value = get_post_meta($post_id, $key, true);
return $custom_value;
}
add_shortcode(\'product\', \'product_func\');
它显示了获取`自定义字段\'所需的每个值,应如下所示:
因此,在我的情况下,关键是:
$key = "MY CUSTOM FIELD";