Posted By

cerxx on 09/04/19


Tagged

wordpress


Versions (?)

Get the Value of the Custom Field Using the Shortcode


 / Published in: PHP
 

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

Get the Value of the Custom Field Using the Shortcode

  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');

Report this snippet  

You need to login to post a comment.