Fatih Toprak
Fatih Toprak

Reputation: 1007

get_post_meta data with shortcode, is that possible

The question is about Wordpress custom meta fields. If my meta field has something in it, I want to display my meta values on my single.php, where I want to. I think it's possible with shortcodes.

function themedemocode($atts,$content = null ) {
return 'What should i do for displaying my meta values ?'; 
}
add_shortcode('demo', 'themedemocode');

My meta fields ids are ; ThemeDemoUrl and ThemeDownloadUrl ?

-- SOLVED --

function examplewpkeep($atts, $content = null ) {
     global $post;
     $custom_content = get_post_custom($post->ID);
     if (isset($custom_content["UygulamaDemo"])) {
        $meta_content = $custom_content["UygulamaDemo"][0];
     }
     if (isset($custom_content["UygulamaKaynak"])) {
        $meta_content = $custom_content["UygulamaKaynak"][0];
     }
     $meta_content  = '<p style="text-align:center;width:600px;"><a class="informational-link" style="color:#fff;text-decoration:none;margin-right:20px;" href="http://www.fatihtoprak.com/git/'.$custom_content["UygulamaDemo"][0].'" target="_blank" />Önizleme.</a><a class="website-link" style="color:#fff;text-decoration:none;" href="http://www.fatihtoprak.com/git/'.$custom_content["UygulamaKaynak"][0].'" target="_blank" />Dosyaları indir.</a></p>';

     return $meta_content;
}
add_shortcode('kaynak', 'examplewpkeep');

Upvotes: 2

Views: 2853

Answers (1)

SunnyRed
SunnyRed

Reputation: 3545

Untested, but something like this should work:

function themedemocode($atts, $content = null ) {
     global $post;
     $meta_content = '';
     $custom_content = get_post_custom($post->ID);
     if (isset($custom_content["ThemeDemoUrl"])) {
        $meta_content .= $custom_content["ThemeDemoUrl"][0];
     }
     if (isset($custom_content["ThemeDownloadUrl "])) {
        $meta_content .= $custom_content["ThemeDownloadUrl"][0];
     }
     return $meta_content;
}

Of course you can wrap your meta content like the following to apply some html / styling to it

$meta_content .= '<p>' . $custom_content["someKey"][0] . '</p>'.

Upvotes: 3

Related Questions