0

I have seen many asking how to insert custom Fields in Wordpress post? Even though plugins are available nothing is a single-click solution.We need to do a bit of adjustments to make it work.Today I ended up doing that to improve the appearance and usability of my theme as it didn't have those built in options.Since,I struggled a lot to make it work I thought of sharing it here.

1 Answer 1

1

Here are the main steps:

Install the Custom Field Template. Navigate to Appearance > Editor and include this code snippet in your functions.php file:

function getCustomField($theField) {
    global $post;
    $block = get_post_meta($post->ID, $theField);
    if($block){
        foreach(($block) as $blocks) {
            echo $blocks;
        }
    }
}

Configure the custom fields by going to Settings > Custom Field Template, expand the template content section, and add something like this:

[Day Number]
type = text
size = 10
output = true

Note: Unless you add output = true, the custom fields won’t work.

Go to Appearance > Editor and add this code into the template (such as single.php) where you want the value of the custom field to appear:

To add a conditional statement around the getCustomField function (so that it only appears under certain conditions, such as the post being in a specific category), include this before the getCustomField function:

… then insert your getCustomField functions …

and then close with this: Hope this helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.