How to store custom field value in variable [WordPress]

Friday, 4 July 2014

In WordPress blog we use custom fields for retrieving values from posts and pages. Here we will see how to store custom values in variables.
We have custom field for example:
Custom Field Name:SingleProduct
Custom Field Value:12345
Now we will see how we can store values using get_post_custom_values(“Custom Field Name”) function.
<?php
  $values = get_post_custom_values("SingleProduct");
  if (isset($values[0])) {
     $SingleProduct = $values[0];
} ?>

Here $values is array. get_post_custom_values function stores custom field values in array.
And then the $values[0] is stored in $SingleProduct variable.

0 comments:

Post a Comment