Guide to understanding Meta Data and when to use it
What are Custom Fields?
In simple words custom fields are additional information that you can add in Posts and Pages, as well as to any custom post type defined to your system. A good starting point is to check the WordPress Codex, where custom fields are described in details and you can find some examples.In our tutorial we will add Product Name and Product price to the post and we will display it bellow the post title. Also we will make price discount for our registered users.
Setting the stage
We have to enable (if not enabled already) Custom Fields from Screen Options in our Post Editor.When we are ready the Custom Fields Area should be visible right bellow our WYSIWYG post editor.
Adding our first Custom Field
Go to Custom Fields and click the “Enter New” button.In the “Name“field we should define the name of our Custom Filed (in our case “product_name“) and in “Value” – our custom filed’s value (for instance, “Butterflies“).
Now, click on “Add Custom Filed” button and there we go – we have created our first custom filed.
We can repeat step two and do the same, but this time we will add product price.
Again we go to Custom Fields, click Enter New. For Name add product_price and for Value add 100.
Now we have our two Custom Fields and we can proceed to the next step.
Display our Custom Fields below Post title.
For the sake of our example we would be using a basic WordPress installation and display our content based on the Twenty Eleven theme.Now go to Appearance => Editor and from Twenty Eleven files choose content-single.php(or single.php, it depends of your WordPress theme).
We have to display Product Name and Product Price bellow Post title, so will place our code somewhere bellow Post title.
We will use get_post_meta() WordPress and update_post_meta() functions.
<?php
/*
* We will use this place for our Custom Fileds
*/
// set defaults for our name and price should they be empty
$product_name_def = ‘Default name’;
$product_price_def = 0;
$product_name = get_post_meta($post->ID, ‘product_name’, true);
// check if the custom field has a value
if (empty ( $product_name )) {
$product_name = $product_name_def;
}
$product_price = get_post_meta($post->ID, ‘product_price’, true);
// check if the custom field has a value
if ( empty( $product_price ) ) {
$product_price = $product_price_def;
}
$vat = 1.2;
$product_price_vat = ($product_price * $vat);
$product_price_updated = update_post_meta($post->ID, ‘product_price_vat’, $product_price_vat);
if (is_user_logged_in()) {
echo ‘The price of <strong>’. $product_name .’</strong> is: <strong>$’. $product_price .’</strong>.’;
} else {
echo ‘The price of <strong>’. $product_name .’</strong> is: <strong>$’. $product_price_vat .’</strong>.’;
}
?>
Let’s revise the sample code above.
We use the get_post_meta($post_id, $key, $single); function.
where:
$post_id – The ID of the post from which you want the data. Use $post->ID to get a post’s ID.
$key – A string containing the name of the meta value you want.
$single – this is a boolean variable. When it’s set to true, it returns the value as a string. A custom field can have multiple values, in which case you would set this variable to false, returning an array instead.
And also we use update_post_meta($post_id, $meta_key, $meta_value);
where:
$post_id - The ID of the post from which you want the data. Use $post->ID to get a post’s ID.
$meta_key – is the key of the custom field you will edit.
$meta_value – is the new value of the custom field.
Using the is_user_logged_in() function creates a scenario for our sample where logged users of ours see the flat price of our product where regular visitors are only able to see the price with VAT added.
The Result
Logged user:Regular user (non-logged):
And in our post editor you will notice that we have third custom filed product_price_vat added automatically from WordPress.
No hay comentarios:
Publicar un comentario