• Resolved Zhen

    (@milohuang)


    Greeting all,

    Am working on my project and trying to put the custom fields to the shortcode so I can use it in my posts.

    add_shortcode('shortcodename', 'shortcodefunction');
    function shortcodefunction() {
        return get_post_meta($post->ID, 'Description', true);
    }

    I want to return the value of the custom field “Description” but nothing shown. Wonder if there is anything wrong with the code above. And also wondering how I can add several custom fields to the shortcode, for example:

    add_shortcode('shortcodename', 'shortcodefunction');
    function shortcodefunction() {
        return '<div style="border:1px dotted #000; text-align:center; padding:10px;">
            <h4>'.get_post_meta($post->ID, 'Description', true).'</h4>
            <p><a href="'.get_post_meta($post->ID, 'URL', true).'">Stay up to date, and subscribe to my RSS feeds!</a></p>
            </div>';
    }

    Thank you very much!

Viewing 1 replies (of 1 total)
  • Thread Starter Zhen

    (@milohuang)

    Oops! It seems that I have got it.

    Added the following code to the function and it works:
    global $post;

Viewing 1 replies (of 1 total)
  • The topic ‘How to add custom field in shortcode?’ is closed to new replies.