カテゴリに画像登録 出力コード等 タクソノミー

http://kotori-blog.com/wordpress/cat_customfields/
参考

■アドバンスカスタムフィールド
タクソノミータームを選び
返り値 ID

■画像登録

■コード出力

ページ 一覧出力 フォーチ

ポイントは

1
2
'taxonomy'                 => 'bali-tours-cat',
$post_id = 'bali-tours-cat_'.$cat_id;

ポストIDにタクソノミー名アンダーバー+ID
でそのカテゴリに入力されたもののポストIDがでる

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'bali-tours-cat',
    'pad_counts'               => false
);
$categories = get_categories($args);
//$categories = get_categories('parent=0'); 親カテゴリーのみ
foreach($categories as $category) :
  
$cat_id = $category->cat_ID;
$post_id = 'bali-tours-cat_'.$cat_id;
    
$catimg = get_field('cat_img',$post_id);
$img = wp_get_attachment_image_src($catimg, 'full');
?>
<div>あ<?php echo $category->cat_name; ?></div>
<div><a href="/bali-tours-cat/<?php echo $category->category_nicename; ?>"><img src="<?php echo $img[0]; ?>" alt="<?php echo $category->cat_name; ?>" /></a></div>
<?php endforeach; ?>

カテゴリアーカイブ出だすには

1
2
3
4
5
6
7
8
9
<?php
$cat_id = get_queried_object()->cat_ID;
$post_id = 'category_'.$cat_id;
  
$catimg = get_field('cat_img',$post_id);
$img = wp_get_attachment_image_src($catimg, 'full');
?>
<div><?php single_cat_title(); ?></div>
<div><img src="<?php echo $img[0]; ?>" alt="<?php single_cat_title(); ?>" /></div>