やりたいこと
固定ページにショートコードを埋め込み
カテゴリIDごとに表示を変更したい
###できないこと
function.phpから global $postを
外部ファイルに渡し
投稿IDを取得して、get_the_categoryを使用して
カテゴリ名を取得しようとしているが
get_the_categoryが取得できない。
function
1function hoge() { 2 global $post; 3 $_GET['post'] = $post; 4 ob_start(); 5 get_template_part('hoge/hoge'); 6 return ob_get_clean(); 7} 8add_shortcode('hoge', 'hoge');
hoge
1<?php $post = $_GET['post']; 2$category = get_the_category($post->ID); ?> 3<?php $cat_id = $category[0]->cat_ID; ?> 4 5<?php $cat = get_term($cat_id); ?> 6<h2><?php echo $cat -> name; ?></h2>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。