Forums

Conditional Meta Data & Custom Fields (10 posts)

  1. mfal55
    Member
    Posted 1 year ago #

    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.
  2. stvwlf
    Member
    Posted 1 year ago #

    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();
       }
  3. mfal55
    Member
    Posted 1 year ago #

    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();
       }
     ?>
  4. stvwlf
    Member
    Posted 1 year ago #

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

  5. mfal55
    Member
    Posted 1 year ago #

    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.

  6. stvwlf
    Member
    Posted 1 year ago #

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

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    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();
       }
    ?>
  8. mfal55
    Member
    Posted 1 year ago #

    Keesiemeijer - that's exactly it! Works perfectly now.

    Thanks to both of you for your help!

  9. stvwlf
    Member
    Posted 1 year ago #

    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") {

  10. mfal55
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic