0

I'm trying to write an if statement to check for post_content and if there is no post_content then to grab a textarea field content. Below is my code and with this, at this point it's only rendering "Array" to the page. Any help is greatly appreciated. Thank you!

CODE:

<?php
foreach ($partners as $partner) {
  $a_description = get_post_meta($partner->ID, "a_description");
?>

  <div class="partner-content">
    <?php
      if ($partner->post_content && !$a_description) { ?>
        <?php echo $partner->post_content  ?>
      <?php } else { ?>
        <?php echo get_post_meta($partner->ID, $a_description, true); ?>
      <?php } ?>
  </div>
<?php
}
?>
  • 1
    Please correct your syntax. Never use shorttags. Use proper opening and closing php tag, for example either <?php or ?> – Pieter Goosen Sep 11 '14 at 5:36
  • @PieterGoosen as in <?php echo $partner->post_content ?>? – tjoenz Sep 11 '14 at 5:40
  • 1
    Correct. Shorttags are annoying as not all enviroments are setup for those. As soon as someone copy the content to their test enviroment to test the code, they immediatly get a parse error. Annoying to first go and correct something simple like php tags – Pieter Goosen Sep 11 '14 at 5:49
  • @PieterGoosen thanks for that :) I am new to PHP and Wordpress and love to know what is best practices. I'm still wondering how to get the if statement to work though. Thanks! – tjoenz Sep 11 '14 at 5:51
0

The following line is wrong

<?php echo get_post_meta($partner->ID, $a_description, true); ?>

If you look closely, you are actually passing

get_post_meta($partner->ID, "a_description")

to the $key parameter of get_post_meta as this is the value assigned to $a_description

You should most probably change $a_description to just a_description

EDIT

This line

<?php echo get_post_meta($partner->ID, $a_description, true); ?>

should most probably be

<?php echo get_post_meta($partner->ID, 'a_description', true); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.