Skip to content

Instantly share code, notes, and snippets.

@chocotaro

chocotaro/category_template Secret

Created Jan 20, 2020
Embed
What would you like to do?
汎用カテゴリページ(子カテゴリにも使用可能)
<?php get_header(); ?>
<?php
$category = get_the_category();
$category = $category[0];
$cat_id = $category->cat_ID;
$cat_name = $category->cat_name;
$cat_slug = $category->category_nicename;
$parent = get_category($category->parent);
$parent_cat_name = $parent->cat_name;
$cat = get_category( get_query_var("cat") );
?>
<!-- category -->
<section>
<div class="Container cate">
<div class="contents <?php echo $parent_cat_name; ?> clearfix">
<h2><?php echo $cat->name ; ?></h2>
<dl>
<?php if(have_posts()): while(have_posts()): the_post();
$cat = get_the_category();
$catname = $cat[0]->cat_name; //カテゴリー名
$catslug = $cat[0]->slug; //スラッグ名
?>
<dt><?php the_time('Y年n月j日'); ?><span class="cat-<?php echo $catslug; ?>"><a href="<?php echo home_url(); ?>/archives/category/<?php echo $catslug; ?>"><?php echo $catname; ?></a></span></dt>
<dd><a href="<?php the_permalink(); ?>">
<?php
if(mb_strlen($post->post_title, 'UTF-8')>30){
$title= mb_substr($post->post_title, 0, 30, 'UTF-8');
echo $title.'…';
}else{
echo $post->post_title;
}
?></a></dd>
<?php endwhile; endif; ?>
</dl>
<?php
// リンクが無い場合はNULLが返ってくる
$prev_link = get_previous_posts_link('前のページ');
$next_link = get_next_posts_link('次のページ');
if ( isset( $prev_link ) or isset( $next_link ) ) {
echo '<ul class="pagination">', PHP_EOL;
if( isset( $prev_link ) ) {
echo '<li class="back">',$prev_link,'</li>', PHP_EOL;
}
if( isset( $next_link ) ) {
echo '<li class="next">',$next_link,'</li>', PHP_EOL;
}
echo '</ul>', PHP_EOL;
}
?>
</div>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.