<div class="relatedposts">
<h4>関連記事</h4>
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag)
$tag_ids[] = $individual_tag->term_id;
$args = array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>6, // 表示する関連記事の数
'caller_get_posts'=>1,
'orderby' => 'rand', // 過去記事に内部リンクできる
);
$my_query = new wp_query( $args );
?>
<div class="row">
<?php
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="col-xs-6 inner">
<h5>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ): // サムネイルを持っているとき ?>
<?php echo get_the_post_thumbnail($post->ID, 'thumb100'); //サムネイルを呼び出す?>
<?php else: // サムネイルを持っていないとき ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/no-image.gif" alt="NO IMAGE" title="NO IMAGE"/>
<?php endif; ?>
<?php if (strlen($post->post_title) > 50) {
echo mb_substr(the_title($before = '', $after = '', FALSE), 0, 50, 'UTF-8') . '...'; }
else {
the_title();
} ?>
</a>
</h5>
<p>
<?php
$cat = get_the_category(); $cat = $cat[0]; echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '">';
echo $cat->cat_name;
echo '</a>';
?>
</p>
</div><!--col-xs-6 inner-->
<?php } // while
?>
</div><!--row-->
<?php
} // IF
$post = $orig_post;
wp_reset_query(); ?>
</div><!--relatedposts-->