Get or Display Custom Field on WordPress

<?php echo get_post_meta(get_the_ID(), 'fieldname', TRUE); ?>

Replace fieldname with your custom field name.

This is a PHP code snippet that displays a custom field value from a WordPress post. Here's an explanation of what it does:

  • <?php opens a PHP code block

  • echo prints output to the page

  • get_post_meta() is a WordPress function that retrieves meta data associated with a post. It takes 3 parameters:

    • get_the_ID() - Gets the ID of the current post in the WordPress loop
    • 'fieldname' - The name of the custom field to retrieve (replaced with actual custom field name)
    • TRUE - Return a single value instead of an array
  • So this line retrieves the value of a custom field called 'fieldname' from the current post in the loop, and echoes it out to display on the page.

  • The result is that whatever value is saved in the 'fieldname' custom field for the current post will be outputted where this code is used.

So in summary, it's displaying the value of a custom field called 'fieldname' from the current post in a WordPress theme. The custom field name would be replaced with the actual name you gave the custom field.

Next Post Previous Post
No Comment
Add Comment
comment url