Created
October 13, 2017 16:10
-
-
Save devuri/0ffecd8c9e6a0efc6e495926d3def5c1 to your computer and use it in GitHub Desktop.
a shortcode That Returns Post Meta (Custom Fields)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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