2

我在 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']));
  }
}

我怎样才能获取这个插入的数据?

4

1 回答 1

0

您可以使用此功能代替您的:

function products_save_postdata($post_id)
{
  $array = array();

  if (isset ($_POST['price'])) {
    update_post_meta($post_id, 'price', esc_attr($_POST['price']));
    $array['price'] = $_POST['price'];
  }
  if (isset ($_POST['product_name'])) {
    update_post_meta($post_id, 'product_name', esc_attr($_POST['product_name']));
    $array['product_name'] = $_POST['product_name'];
  }
  if (isset ($_POST['size'])) {
    update_post_meta($post_id, 'size', esc_attr($_POST['size']));
    $array['size'] = $_POST['size'];
  }
  return $array;
}

通过以下方式获取值:

$post_data = products_save_postdata($post_id);

echo $post_data['price'];
echo $post_data['product_name'];
echo $post_data['size'];
于 2013-08-22T06:52:38.930 回答