• Resolved jasonsweb

    (@jasonsweb)


    Hi,

    I’m trying to build a staff page for my theme. The staff members can enter their social media profiles, like twitter, facebook, etc. In total there are 10 custom fields.

    In my single-staff.php I would like to display all the staff members info.
    At the moment I use this code:

    <?php $custom = get_post_custom($post->ID); // Get all custom fields ?>
    <?php if ($custom['staff_twitter'][0] != '') { // Display Twitter ?>
    <a href="https://www.twitter.com/<?php echo $custom['staff_twitter'][0]; ?>">Twitter</a>
    <?php } ?>

    So the link will only show up, if a staff member entered a twitter url. But when a member leaves the twitter field blank, I’m getting this error in WP_Debug mode:
    Notice: Undefined index: tw_staff_twitter

    Is there something I’m doing wrong?

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You can test if the key exists and has a value with this:

    if (isset($custom['staff_twitter'][0]) && $custom['staff_twitter'][0]) {
    // Display Twitter
    }

    Or use get_post_meta():

    $twitter = get_post_meta($post->ID, 'staff_twitter', true);
    if($twitter != '') {
    // Display Twitter
    }

    Thread Starter jasonsweb

    (@jasonsweb)

    Thanks for the fast reply!

    I’m going for the first option, so I don’t have to load the custom fields one by one.
    It works perfect. Thanks!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you got it resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_post_custom value’ is closed to new replies.