Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active December 28, 2015 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mypacecreator/7496805 to your computer and use it in GitHub Desktop.
Save mypacecreator/7496805 to your computer and use it in GitHub Desktop.
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