我创建了一个自定义帖子类型:“products”。在此帖子类型中,有六个自定义字段,其中包含从WP All Import自动导入的信息。
我想使用一个短代码从这6个自定义字段中检索信息。理想情况下,可以使用post的slug来检索信息,因此(“代码”就是slug):
[product code="12345678"]
上面应该查找带有slug 12345678的“product”,然后从这六个自定义字段输出信息。我应该如何处理这个问题?
我创建了一个自定义帖子类型:“products”。在此帖子类型中,有六个自定义字段,其中包含从WP All Import自动导入的信息。
我想使用一个短代码从这6个自定义字段中检索信息。理想情况下,可以使用post的slug来检索信息,因此(“代码”就是slug):
[product code="12345678"]
上面应该查找带有slug 12345678的“product”,然后从这六个自定义字段输出信息。我应该如何处理这个问题?
获取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";
我知道如何使用短代码,甚至制作它们,但我需要了解的是,短代码是作为帖子内容存储在数据库中的纯文本,所以像这样的纯文本如何转换为动态文本。我想知道的是x 在服务器将文本发送到浏览器以使其正常工作之前,是否可以处理文本?