Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active Dec 28, 2015
Embed
What would you like to do?
single.phpで、カテゴリースラッグを元にタイトル画像を表示する(子カテゴリーの場合も、親カテゴリーの画像を使う)
<?php
$cat = get_the_category();
if ($cat[0]->category_parent): //子カテゴリの場合
$parent_cat = get_category($cat[0]->category_parent);
?>
<p class="title"><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo $parent_cat->slug; ?>.jpg" alt="<?php echo $parent_cat->name; ?>" /></p>
<p class="subtitle"><?php echo $cat[0]->cat_name; ?></p>
<?php else: //親カテゴリの場合 ?>
<p class="title"><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo $cat[0]->category_nicename; ?>.jpg" alt="<?php echo $cat[0]->cat_name; ?>" /></p>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment