Return to Snippet

Revision: 77238
at September 4, 2019 02:46 by cerxx


Initial Code
function shortcode_field($atts){
		extract(shortcode_atts(array(
		'post_id' => NULL,
		), $atts));
		if(!isset($atts[0])) return;
		$field = esc_attr($atts[0]);
		
		global $post;
		
		if(gettype($post) == "array"){
			$post_id = 1*(NULL === $post_id) ? $post['ID'] : $post_id;
		} else {
			$post_id = 1*(NULL === $post_id) ? $post->ID : $post_id;
		}
		
		$answer = get_post_meta($post_id, $field, true);
		$answer = $answer;
		
		return $answer;
	}
	add_shortcode('field', 'shortcode_field');

Initial URL
https://wpincode.com/kak-vypolnyat-shortkody-v-proizvolnyx-polyax-v-wordpress/#highlighter_338951

Initial Description
Get the Value of the Custom Field Using the Shortcode

Initial Title
Get the Value of the Custom Field Using the Shortcode

Initial Tags
wordpress

Initial Language
PHP