Get the Value of the Custom Field Using the Shortcode


/ Published in: PHP
Save to your folder(s)

Get the Value of the Custom Field Using the Shortcode


Copy this code and paste it in your HTML
  1. function shortcode_field($atts){
  2. extract(shortcode_atts(array(
  3. 'post_id' => NULL,
  4. ), $atts));
  5. if(!isset($atts[0])) return;
  6. $field = esc_attr($atts[0]);
  7.  
  8. global $post;
  9.  
  10. if(gettype($post) == "array"){
  11. $post_id = 1*(NULL === $post_id) ? $post['ID'] : $post_id;
  12. } else {
  13. $post_id = 1*(NULL === $post_id) ? $post->ID : $post_id;
  14. }
  15.  
  16. $answer = get_post_meta($post_id, $field, true);
  17. $answer = $answer;
  18.  
  19. return $answer;
  20. }
  21. add_shortcode('field', 'shortcode_field');

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.