2013-01-12 17 views
2

這個問題是關於WordPress的自定義元字段。如果我的元字段中有內容,我想在我想要的single.php上顯示我的元值。我認爲這是可能的簡碼。get_post_meta數據與短代碼,這是可能的

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

我的元字段id是; ThemeDemoUrl and ThemeDownloadUrl

- 解決 -

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

回答

3

未經檢驗的,但這樣的事情應該工作:

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; 
} 

當然,你可以用你的元內容,如下面的應用一些HTML /造型它

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

SunnyRed thans快速和偉大的答案我加了f ull和修改後的代碼 - 問題底部 –

+0

不客氣:) – SunnyRed