Create Custom Fileds in Wordpress

Without using any plugin we can create custom fields in WordPress.

In WordPress dashboard at top right corner you can see "Screen options" . Click on it and check "Custom Fields" to activate it . (But you can see "Custom Fields" option only in Post/Page Edit mode)

Custom Fields




In Page Edit Mode under content Editor you can see a Meta box "Custom Fields", In this we can see 2 fields "Name" and "value". "Name" will be your ID and Value will be the output. Create a custom field and update your post or page .

Display custom field

1. <?php the_meta(); ?> //  this displays all custom fields created in a page

place this in your header.php . I am using this as unique Slogan for every page.  

2.If you wanted to display value of one specific custom field, use the code shown below

<?php echo get_post_meta($post->ID, 'demo', true); ?> // "demo" would be ID of custom field

3. Display multiple values of same custom field ID, use this code

<?php $c_demo = get_post_meta($post->ID, 'demo', false); ?> // "demo" would be ID of custom field
<ul>
    <?php foreach($c_demo as $c_demo_display) {
        echo '<li>'.$c_demo_display.'</li>';
    } ?>
</ul>

4. Display output only if custom field exists

<?php

$logic = get_post_meta($post->ID, 'demo', true);

if($logic!='')
{
echo $logic;
}
?>







No comments:

Post a Comment