Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active Aug 29, 2015
Embed
What would you like to do?
【ワードプレス】カテゴリページで小カテゴリがあれば子カテゴリ一覧、なければ記事一覧を表示する
<?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