Skip to content

Instantly share code, notes, and snippets.

@rocket-martue
Last active December 3, 2021 06:08
Show Gist options
  • Save rocket-martue/8ca484a050348133b5556594e3bfa605 to your computer and use it in GitHub Desktop.
Save rocket-martue/8ca484a050348133b5556594e3bfa605 to your computer and use it in GitHub Desktop.
Snow Monkey で複数のカテゴリーラベルを表示する
<?php
/**
* disable .c-entry-summary__term
* 各投稿一覧で デフォルトの taxonomy ラベルを削除
*
* @param string $html はコンテンツの中身.
*/
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/term/term',
function ( $html ) {
$html = NULL;
return $html;
}
);
/**
* 投稿一覧で taxonomy のラベルをタイトルの上に表示
*
* @param string $html はコンテンツの中身.
*/
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
function ( $html ) {
$taxonomy = 'category';
$tags = '';
$postid = get_the_ID();
$post_terms = get_the_terms( $postid, $taxonomy );
$published = get_the_time( get_option( 'date_format' ) );
$timestamp = get_the_time( 'c' );
if ( ! empty( $post_terms ) ) {
$tags = '<ul class="' .$taxonomy. '-tags taxonomy-tags">';
foreach ( $post_terms as $post_term ) {
$term_name = $post_term->name;
$slug = $post_term->slug;
$term_id = $post_term->term_id;
$tags .= '<li class="' .$taxonomy. '-' .$term_id.' ' .$slug.' tags">' . $term_name . '</li>';
}
$tags .= '<li class="published"><i class="far fa-clock" aria-hidden="true"></i><span class="screen-reader-text">投稿日</span><time datetime="'. $timestamp .'">'. $published .'</time></li>';
$tags .= '</ul>';
} else {
$tags = '<ul class="taxonomy-tags">';
$tags .= '<li class="published"><i class="far fa-clock" aria-hidden="true"></i><span class="screen-reader-text">投稿日</span><time datetime="'. $timestamp .'">'. $published .'</time></li>';
$tags .= '</ul>';
}
$html = str_replace( '</h2>', '</h2>' .$tags, $html );
return $html;
}
);
@rocket-martue
Copy link
Author

rocket-martue commented Dec 3, 2021

CSS

.taxonomy-tags {
  list-style: none;
  padding-left: 0;
}

.taxonomy-tags .tags {
  background: #008a20;
  border-radius: 4px;
  color: #fff;
  display: inline-block;
  font-size: 0.7em;
  /* font-weight: 700; */
  line-height: 1;
  margin-right: 0.5rem;
  margin-bottom: .5rem;
  padding: .4rem;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment