• Hi there,
    I’m trying to set up some conditional code, where I’ve set up some custom fields and if they are used, I don’t want specific post meta data to display. The field I have set up is Byline, which is entered if the article isn’t by any of the authors of the system. This is the code I have so far. I can get the custom field to show up, but I can’t get the author to show up if no custom field is used. Can anyone tell me what I’m doing wrong?

    <?php
    if ($key="Byline") {
          get_post_meta($post->ID, $key, true);
       } else {
          echo 'the_author()';
       }
     ?>
    
    Thanks in advance for your help.
Viewing 9 replies - 1 through 9 (of 9 total)
  • for one thing your code for the_author is wrong. I didn’t even look at the rest of it

    it should be

    } else {
          the_author();
       }

    Thread Starter mfal55

    (@mfal55)

    Thanks – I had it that way too, but it didn’t work. The code below manages to show the custom field when there is one used, but the else doesn’t show just the author when there isn’t the custom field used. I feel like I’m missing something simple that I’m just not seeing.

    <?php
    if ($key="Byline") {
          echo get_post_meta($post->ID, $key, true);
       } else {
          the_author();
       }
     ?>

    The code you are using has to be run with the WordPress loop. Is that where you are running this?

    Thread Starter mfal55

    (@mfal55)

    I’m adding this to the single.php page. I’m within the loop and am working on it just above <?php the_content(); ?>. I’m not getting any errors, the else just doesn’t show.

    for testing purposes, what happens if you put the_author() before the if statement?

    Moderator keesiemeijer

    (@keesiemeijer)

    the if ($key="Byline") part gives the $key variable a value of “Byline” so that is always true. To test if a post has a custom field “Byline” try it with this:

    <?php
    $key = get_post_meta($post->ID, 'Byline', true);
    if ($key != '') {
          echo $key;
       } else {
          the_author();
       }
    ?>

    Thread Starter mfal55

    (@mfal55)

    Keesiemeijer – that’s exactly it! Works perfectly now.

    Thanks to both of you for your help!

    Actually I see what the problem is

    you have
    if ($key="Byline") {
    You are assigning the value Byline to $key every time the code runs

    What you mean is compare the value of $key with the string ‘Byline’
    the first line should be
    if ($key=="Byline") {

    Thread Starter mfal55

    (@mfal55)

    Thanks stvwlf! Keesiemeijer had the solution. I appreciate your help. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Conditional Meta Data & Custom Fields’ is closed to new replies.