Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active August 29, 2015 14:25
Show Gist options
  • Save kurozumi/4af6a70b74335f773d05 to your computer and use it in GitHub Desktop.
Save kurozumi/4af6a70b74335f773d05 to your computer and use it in GitHub Desktop.
【ワードプレス】カテゴリページで小カテゴリがあれば子カテゴリ一覧、なければ記事一覧を表示する
<?php
$categories = get_categories(array(
'parent' => get_queried_object()->term_id
));
if(!$categories){
$wp_query = new WP_Query(array(
'cat' => get_queried_object()->term_id
));
}
get_header();
?>
<div id="content">
<?php if (have_posts()) : ?>
<?php if ($categories): ?>
<ul>
<?php foreach ($categories as $category): ?>
<li><a href="<?php echo esc_url(get_category_link($category->cat_ID)); ?>"><?php echo $category->name; ?></a></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php else : ?>
<p>none</p>
<?php endif; ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment