• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2784

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Consultation
  • Full Access
  • Log in

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 ); 
    }
  }
}

Custom Fields

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.