• 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

Print Custom Field Names And Values

This code enables you to efficiently output a specific list of custom field names and values conditionally. The custom field name and value are only printed if the value for each name exists.

The code uses the foreach loop function to display your custom field names with values rather than individual calls to each custom field so its more efficient.

Here’s the front end output without any CSS:

custom-field-loop

This example enables you to print a list of specific custom fields without printing all custom fields for each post. In this example the data is hooked after the content of each single post, if it exists. If empty, there is no output.

Here’s the code for logged in members:

Register for full access

Custom Fields

Reader Interactions

Comments

  1. Tatiana M says

    April 20, 2018 at 8:21 am

    Hi Brad,
    If I wanted to add custom fields before the content on a custom post type (dogs), how would I go about modifying this code to do that? Basically, I’d like to use this template
    http://hoofnpawsamoyeds.com/dog/pop/ but modify it to work with genesis child themes instead. There are repeating custom fields in three different sections: below the featured image (Awards), the main information about the dog, in this case (Pop), and the table below it, (Notable Offspring). I’ve tried looking through the code for this theme, but the parts that I want are scattered throughout in different files and I don’t know where to begin to convert it to work with genesis.

    If you need to look at the code, I can upload the entire theme and send it to you to work with.

    I do also own ACF Pro, if it is easier to work with a plugin in this instance.

    Any help is appreciated (I couldn’t find a way to email you directly with this request, so I’m posting it in the comments. Hope that’s okay!

    Log in to Reply
    • Brad Dalton says

      April 20, 2018 at 8:33 am

      Use is_singular(‘dog’) with any genesis hook https://wpsites.net/genesis-hooks/.

      add_action( 'genesis_before_content', 'function_name' );
      function function_name() {
      
      if ( is_singular('dog') )
      
          $value = get_post_meta( get_the_ID(), 'key', true );
          
          if ( ! empty( $value ) ) {
          
          echo '<div class="your-class">'. $value .'</div>';
          
          }
      }

      Or, add the code to a single-dog.php file without the conditional tag.

      Log in to Reply

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.