How to display all custom fields (name AND value) in a post?

You are working on a custom WordPress theme that uses lots and lots of custom fields and you suddenly want to see what all custom fields are assigned to any post.  You can do by going into the dashboard of the site and editing the specific post but what if you want to show it on the front-end of site on the single post template?

What if you have tons of custom fields and just want to show them all on the single post without specifying specific custom field name/key?

In that case you can use the snippet below inside the post loop to show all the custom fields that are attached to a post:

<?php
$custom_fields = get_post_custom();

foreach ( $custom_fields as $field_key => $field_values ) {
	foreach ( $field_values as $key => $value )
		echo $field_key . ' - ' . $value . '<br />';
}
?>

Source

Leave a Reply