Skip to content

Instantly share code, notes, and snippets.

@devuri
Created October 13, 2017 16:10
Show Gist options
  • Select an option

  • Save devuri/0ffecd8c9e6a0efc6e495926d3def5c1 to your computer and use it in GitHub Desktop.

Select an option

Save devuri/0ffecd8c9e6a0efc6e495926d3def5c1 to your computer and use it in GitHub Desktop.
a shortcode That Returns Post Meta (Custom Fields)
<?php
//https://wpexplorer-themes.com/total/snippets/meta-shortcode/
/**
* Returns a custom field value
*
* Usage [custom_field id="staff_position"]
*
* Available options... staff_position, staff_twitter, staff_facebook, staff_email...etc
* See framework/classes/metabox.php for all meta id's
*/
function my_display_custom_field( $atts ) {
$atts = extract( shortcode_atts( array(
'id' => '',
), $atts ) );
if ( ! $id ) return;
$id = 'wpex_'. $id; // prefix the id
$data = get_post_meta( get_the_ID(), $id, true );
if ( $data ) {
return '<span class="wpex-custom-field id-'. $id .'">'. $data .'</span>';
}
}
add_shortcode( 'custom_field', 'my_display_custom_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment