Written by Anonymous
●html-forms.php
if ( !function_exists( 'generate_info_list_tag' ) ):
function generate_info_list_tag($atts){
extract(shortcode_atts(array(
'count' => 5,
'cats' => array(),
'caption' => __( '新着情報', THEME_NAME ),
'frame' => 1,
'divider' => 1,
'modified' => 0,
'offset' => 0,
'action' => null,
'post_type' => 'post',
'taxonomy' => 'category',
), $atts));
$args = array(
'post_type' => $post_type,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'posts_per_page' => $count,
'offset' => $offset,
'action' => $action,
);
// 更新日順
if ($modified) {
$args['orderby'] = 'modified';
}
// インデックス除外カテゴリー
$exclude_category_ids = get_archive_exclude_category_ids();
if ($exclude_category_ids && is_array($exclude_category_ids)) {
$args['category__not_in'] = $exclude_category_ids;
}
// 投稿編集のその他設定「アーカイブに出力しない」を除外
$exclude_post_ids = get_archive_exclude_post_ids();
if ($exclude_post_ids && is_array($exclude_post_ids)) {
$args['post__not_in'] = $exclude_post_ids;
}
/* カテゴリーの指定 */
if ($cats !== 'all') {
$terms = is_array($cats) ? $cats : explode(',', $cats);
$terms = array_map('intval', $terms);
$args['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $terms,
)
);
}
$args = apply_filters( 'get_info_list_args', $args );
$query = new WP_Query( $args );
$frame_class = ($frame ? ' is-style-frame-border' : '');
$divider_class = ($divider ? ' is-style-divider-line' : '');
if( $query->have_posts() ): ?>
<div id="info-list" class="info-list<?php echo $frame_class; ?><?php echo $divider_class; ?>">
<?php if ($caption): ?>
<div class="info-list-caption"><?php echo esc_html($caption); ?></div>
<?php endif; ?>
<?php while ($query->have_posts()) : $query->the_post();
$date = get_the_time(get_site_date_format());
$update_date = get_update_time(get_site_date_format());
if ($modified && $update_date) {
$date = $update_date;
}
?>
<div class="info-list-item">
<div class="info-list-item-content"><a href="<?php the_permalink(); ?>" class="info-list-item-content-link"><?php the_title();?></a></div>
<?php do_action('info_list_item_meta_before'); ?>
<div class="info-list-item-meta">
<span class="info-list-item-date"><?php echo $date; ?></span>
<span class="info-list-item-categorys"><?php the_nolink_categories($taxonomy) ?></span>
</div>
</div>
<?php endwhile; ?>
</div>
<?php else :
echo '<p>'.__( '記事は見つかりませんでした。', THEME_NAME ).'</p>';
endif;
wp_reset_postdata();
}
endif;
●shortcodes.php
add_shortcode('info_list', 'get_info_list_shortcode');
if ( !function_exists( 'get_info_list_shortcode' ) ):
function get_info_list_shortcode($atts){
extract(shortcode_atts(array(
'count' => 5,
'cats' => 'all',
'caption' => __( '新着情報', THEME_NAME ),
'frame' => 1,
'divider' => 1,
'modified' => 0,
'offset' => 0,
'action' => null,
'post_type' => 'post',
'taxonomy' => 'category',
), $atts, 'info_list'));
//countオプションに異常値が入っていた場合
if (!is_numeric($count) || (is_numeric($count) && (intval($count)) <= 0)) {
$count = 5;
}
//カテゴリーを配列化
$cat_ids = array();
if ($cats && $cats != 'all') {
$cat_ids = explode(',', $cats);
}
$atts = array(
'count' => $count,
'cats' => $cat_ids,
'caption' => $caption,
'frame' => $frame,
'divider' => $divider,
'modified' => $modified,
'offset' => $offset,
'action' => $action,
'post_type' => $post_type,
'taxonomy' => $taxonomy,
);
ob_start();
generate_info_list_tag($atts);
$tag = ob_get_clean();
return apply_filters('get_info_list_tag', $tag);
}
endif;
●utils.php
if ( !function_exists( 'the_nolink_categories' ) ):
function the_nolink_categories($taxonomy = 'category'){
echo get_the_nolink_categories($taxonomy);
}
endif;
if ( !function_exists( 'get_the_nolink_categories' ) ) :
function get_the_nolink_categories($taxonomy) {
$categories = null;
// タクソノミーのオブジェクトを取得
$taxonomy_obj = get_taxonomy($taxonomy);
// 階層型タクソノミーでない場合
if ($taxonomy_obj && !$taxonomy_obj->hierarchical) {
return $categories;
}
// 指定されたタクソノミーのタームを取得
$terms = get_the_terms(get_the_ID(), $taxonomy);
if ($terms && !is_wp_error($terms)) {
// ターム名をA-Zにソート
usort($terms, function($a, $b) {
return strcmp($a->name, $b->name);
});
foreach ($terms as $term) {
$id = $term->term_id;
$classes = array(
'entry-category',
'cat-label-' . $id,
);
$classes = apply_filters('nolink_category_label_classes', $classes, $term);
$implode_class = implode(' ', $classes);
$categories .= '<span class="' . $implode_class . '">' . $term->name . '</span>';
}
}
return $categories;
}
endif;