我在wordpress中插入了产品价格、名称和大小的值。插入代码如下
function products_save_postdata($post_id) {
if (isset ($_POST[\'price\'])) {
update_post_meta($post_id, \'price\', esc_attr($_POST[\'price\']));
}
if (isset ($_POST[\'product_name\'])) {
update_post_meta($post_id, \'product_name\', esc_attr($_POST[\'product_name\']));
}
if (isset ($_POST[\'size\'])) {
update_post_meta($post_id, \'size\', esc_attr($_POST[\'size\']));
}
}
如何获取插入的数据?
最合适的回答,由SO网友:Balas 整理而成
<?php
$args = array(\'post_type\'=>\'products\',\'posts_per_page\'=>10,\'number_posts\'=>10);
$products = get_posts($args);
foreach($products as $product) {
$productid = $product->ID;
echo $producttitle = $product->post_title;
echo $productdescription = $product->post_content;
echo get_post_meta($productid ,\'price\', TRUE);
echo get_post_meta($productid ,\'product_name\', TRUE);
echo get_post_meta($productid ,\'size\', TRUE);
}
?>
如果值为空,则显示null,否则为空