Forums

How to? Pre-filled custom fields (5 posts)

  1. Hiperciencia
    Member
    Posted 2 years ago #

    Hi,

    I would like to know how to, when I add a new post, to have some custom fields pre-filled with some specific always same data.

    I usually create a post and later put manually the generic data. Later in the same day, I fill those fields myself. But today I have to add each one with some data that would save me time if it was pre-filled, when I click "add new post".

    Thank you,

  2. MichaelH
    Volunteer
    Posted 2 years ago #

  3. Hiperciencia
    Member
    Posted 2 years ago #

    Thank you Michael!

    Do you know if there is a code, that i can use in the template pages, that reads the custom field to show the image and, if the CF is empty, show a default image?

    I think it you be a better solution.

  4. MichaelH
    Volunteer
    Posted 2 years ago #

  5. mattyza
    Member
    Posted 2 years ago #

    Hi Hiperciencia,
    What you are referring to in your last post would be a PHP conditional. This would look something like this:

    <?php
    $variable_name = get_post_meta($post->ID, 'custom_field_key', true);
    if ($variable_name == '') {
    echo 'THIS IS THE DEFAULT IF THE CUSTOM FIELD IS EMPTY';
    } else {
    echo $variable_name;
    }
    ?>

    The above code uses the get_post_meta() function to get the value of the custom field with the name specified in place of 'custom_field_key'. This value is then checked to see if it's empty. If it is, it displays a default string. Otherwise, it displays the value of the custom field.

    This code is rough and can be refined somewhat. For more information, visit the Codex section of custom fields.

    I hope this helps. :)

    Cheers,
    Matty.

Topic Closed

This topic has been closed to new replies.

About this Topic