カスタムフィールドの内容を取得
- Category: WordPress関係
function my_get_meta($key){
global $post;
return get_post_meta($post->ID,$key,true);
}
$keyはカスタムフィールドのフィールド名。値を取得するだけなので、そのまま表示したい場合はechoする。
function my_get_meta($key){
global $post;
return get_post_meta($post->ID,$key,true);
}
$keyはカスタムフィールドのフィールド名。値を取得するだけなので、そのまま表示したい場合はechoする。
カテゴリアーカイブ内の$catで取得できる値は親カテのIDだから、そのままquery_posts()に値を投げ込むと親カテと子カテの両方にチェック入れてない限り、親カテのアーカイブでは子カテの記事は表示されない。
よって、get_category_children()でその親カテに所属する子カテのIDを取得してquery_posts()に投げ込めば、子カテにしかチェック入れてない記事も親カテのアーカイブに表示させることができる。
補足。get_category_children()で取得できる値は文字列 *1なのでsplit関数 *2explode関数を使って配列形式にしてからquery_posts()に投げ込まないといけない。
//子カテゴリのIDを配列で取得
function get_child_id_Array($parrentID){
$childcat = get_category_children($parrentID);
if($childcat) {
$childcat = explode('/',$childcat);
array_shift($childcat);
return $childcat;
} else {
return array($parrentID);
}
}
$childCat = get_category($cat);
if ($childCat->parent) {
$parentCat = get_category($cat->parent);
}
カテゴリーアーカイブ内での使用を想定して$catと書いたけど、$catに子カテゴリのIDを入れれば別にどこでも使える。あとはecho attribute_escape($parentCat->cat_name);
とかやればカテゴリ名を出力できたり、色々。
var_dump($parentCat);
とかやって$parentCatの中身を出力してみると何が格納されてるのか分かりやすいと思う。$parentCatは配列なのでechoしただけじゃ中身をみられないので注意。
function meta_link() {
if(is_single()) {
global $wpdb, $wp_query;
$post = $wp_query->post;
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo '<link rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" />' . "\n";
}
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo '<link rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" />' . "\n";
}
}
}
テーマ内のfunctions.phpに記述しておいて、適当な箇所で呼び出して使う。
query_posts( array(‘post__not_in’ => array(投稿ID) ); とかなんとかやって配列で除外したい投稿 ID を入れれば OK。当然、$query_stringの中身も入れないと特定記事を除いた全記事が出力されてしまう。
たとえば、カテゴリアーカイブでIDが1と3の記事を除外する場合。
$current_paged = intval(get_query_var('paged'));
$query = array('cat' => $cat , 'post__not_in' => array(1,3) , 'paged' => $current_paged);
query_posts($query);