Get All Values For 1 Specific Custom Field Key

This code enables you to print all the values for a specific custom field. In this example, the custom field name is key_1.

add_action( 'loop_start', 'your_function' ); 
function your_function() {
if ( is_singular('post') ) {
    $key = get_post_custom_values( 'key_1' );
    foreach ( $key as $value ) {
    printf( '%s<br>', $value ); 
    }
  }
}

The code uses the foreach PHP function with get_post_custom_values.

Use this code when you want to display all custom field values.

Here’s the backend setup for each custom field used in this example:

custom-fields

In The Loop

Use this code to avoid any loops added to the sidebars like the Genesis Featured Posts widget or related featured type widgets.

add_action( 'loop_start', 'your_function' ); 
function your_function() {
if ( is_singular('post') && in_the_loop() ) {
    $key = get_post_custom_values( 'key_1' );
    foreach ( $key as $value ) {
    printf( '%s<br>', $value ); 
    }
  }
}

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.