0

I'm new at WordPress and trying to figure out how to put my custom field within the post and not before or after the post. I'm using this piece of code:

<?php if ( get_post_meta($post->ID, 'music', true) ) : ?>

<p><strong>I am Currently Listening to:</strong> <em><?php $key="music"; echo   get_post_meta($post->ID, $key, true);?></em></p> 

<?php endif; ?>

and placing it just before or after While loop in single.php file. If I place code anywhere within While loop - I get this error

Parse error: syntax error, unexpected '<' in /public_html/wp-content/themes/twentyseventeen/single.php on line 36

In general, my custom field is working, just appearing too high or too low and not in a post. Thank You.

developerme
  • 1,845
  • 2
  • 15
  • 34

1 Answers1

1

Inside the while loop, you are already in PHP, so you can use this code in there instead (all I did was remove the PHP tag at the beginning and at the end, and reformat the rest so it is a bit more clear):

// ...we are already in PHP at this point, so we can go right into it:
if ( get_post_meta($post->ID, 'music', true) ) : ?>
     <p><strong>I am Currently Listening to:</strong> <em>
     <?php
          $key="music"; echo get_post_meta($post->ID, $key, true);
     ?>
     </em></p> 
<?php endif;
// ...PHP code continues here
Eric
  • 5,104
  • 10
  • 41
  • 70