Some Author want to assign Some extra Information to his post and do not want to write this information in post description. For this purpose wordpress allow the user to assign custom field to his post. This custom field have extra information known as mata-data. This mata data of post hold the key and value .Key is name of which is same for all post and value can be different for each post.
While writing or updating your post you can see a section name as Custom Field. Here you can Add, update and Delete a Custom Post.
- How to Add Custom Field
To add custom filed Scroll down you will see Add New Custom Field
Form select you can select the existing custom field and put the value in value box.
But if you want to add new custom field then click on Enter new
In name box you can Write the name of field and put value in value box. And click on Add Custom field.
- Update and Delete
In Custom Field Section you can see all the added custom fields. To update a Custom Field click on update and edit the value. For Delete Click on Delete then custom field will remove.
- How insert custom Field in code
After add custom field in post goto the single post page. If you have only one custom field of each post then write this in wordpress loop
// <?php the_meta(); ?>
But if you multiple custom field to show then we access each custom field with it key value which is its name.
// <?php echo get_post_meta($post->ID, ‘key’, true); ?>
In above line post->id is id of post and ‘key’ is name of custom field and third parameter is a Boolean value. This value effect on the return value if value is true then the custom field have single value but if the value is fasle then custom field return an array of values. Like that
/*
<?php $songs = get_post_meta($post->ID, ‘songs’, false); ?>
<h3>This post is inspired by:</h3>
<ul>
<?php foreach($songs as $song) {
echo ‘<li>’.$song.'</li>’;
} ?>
</ul>
*/
In above example we can see that return value is false means it will return an array of multiple values which will stored in $songs. These all value will be showed through foreach loop.