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

WP SITES

2665

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

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

PHP Code

template_include

get_body_class

if else

array

class_exists

foreach

sprintf

add_action

printf

variable

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems