Forums

Conditional custom fields (3 posts)

  1. bheinks
    Member
    Posted 1 year ago #

    Alright, so the website for my high school journalism program has custom fields for the writer's name and their corresponding job title. Here's the code to display them in posts:

    <?php $writer = get_post_meta($post->ID, writer, true); $jobtitle = get_post_meta($post->ID, jobtitle, true); if ($writer) { ?><?php echo $writer; if ($jobtitle) { ?>, <?php echo $jobtitle; }} ?>

    It comes out to look like this: "Writer, Jobtitle". I'd like for these to be conditional with the word "None" for when the post lacks a writer/jobtitle. How could I achieve this? Thanks in advance.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try:

    <?php
    $writer = get_post_meta($post->ID, writer, true);
    $jobtitle = get_post_meta($post->ID, jobtitle, true);
    if ($writer) echo $writer;
    else echo 'None';
    if ($jobtitle) echo $jobtitle;
    else echo 'None';
    ?>
  3. bheinks
    Member
    Posted 1 year ago #

    That did the trick. Thank you very much.

Topic Closed

This topic has been closed to new replies.

About this Topic