blog.knym.net | I'm searching something to change my life.

wordpressでカスタムフィールドの表示

/wp-content/themes/{THEME_NAME}/functions.phpに下記のfunctionを追加

function showPostMeta($key){
    global $post;
    if(get_post_meta($post->ID, $key, true)){
        echo get_post_meta($post->ID, $key, true); 
    }
}
//$keys = Array("customField1","customField2","customField3"
function hasPostMetas($keys){
    foreach($keys as $key){
        if(hasPostMeta($key)) return 1;
    }
    return 0;
}


/wp-content/themes/{THEME_NAME}/page.phpなどで以下のコードを入れとくと出力。

<?php
//custom fieldでscheduleというkeyと値が設定されている場合
showPostMeta("schedule");
?>


Using Custom Fields « WordPress Codex
を参照。


wordpressの管理者画面上の呼び名がcustom fieldなのに、たたく関数名にpost_metaって、混乱しそうだから統一したほうがいいんじゃないかと思った。


カスタムフィールドは値の表示、非表示をphp側で設定できるようになるから重宝してる。
WordPress › Custom Field Template « WordPress Plugins
を使うとやりやすくなってお勧めです。


一緒にfunctions.phpに下記のcustom fieldに値があるかどうかのチェック関数も結構使えてお勧めです。

function hasPostMeta($key){
    global $post;
    return (get_post_meta($post->ID, $key, true))? 1:0;
}

Related Posts

Comment Pages

There are 2 Comments to "wordpressでカスタムフィールドの表示"

Write a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Essentials

Meta

Pages

Categories