<?php get_header(); ?>
<div class="container pt-3">
        <div class="row">
        <div class="col-md-8">
            <div class="content">
                <?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
                
            <h1 class="font-weight-bold"><?php the_title(); ?></h1>
            
                <p class="text-right text-muted"><?php echo get_the_author_link(); ?> <time><?php the_date('Y-m-d'); ?></time></p>
                <article>
                <?php the_content('Read the rest&raquo;'); ?>
                </article>
                <div id="tags" class="mb-2"><?php the_tags("<span class='btn btn-outline-info my-1'>","</span> <span class='btn btn-outline-info my-1'>","</span>"); ?></div>
            </div>
            	<?php endwhile; ?>

            <nav><div class="row border-top border-bottom my-2 p-2"><div class="col-sm-6">&laquo; <?php previous_post_link('%link') ?></div><div class="col-sm-6 text-right"><?php next_post_link('%link') ?> &raquo;</div></div></nav>
            <div class="">
                <div class="row">
                    
                <?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
  foreach ($post_tags as $tag) {
    // 获取标签列表
    $tag_list[] .= $tag->term_id;
  }

  // 随机获取标签列表中的一个标签
  $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];

  // 该方法使用 query_posts() 函数来调用相关文章，以下是参数列表
  $args = array(
        'tag__in' => array($post_tag),
        'category__not_in' => array(NULL),  // 不包括的分类ID
        'post__not_in' => array($post->ID),
        'showposts' => 6,				            // 显示相关文章数量
        'caller_get_posts' => 1
    );
  query_posts($args);

  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
            <div class="col-sm-4"><div class="card mb-2">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><div class="listi2"><img class="card-img-top" src="<?php echo catch_that_image(); ?>" alt="<?php the_title(); ?>"></div>
                    
                        <h5 class="card-title text-dark"><?php echo wp_trim_words(get_the_title(),5); ?></h5></a>
                    </div>
                </div>
<?php
    }
  }
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>

                
                </div>
            </div>
            <?php else : ?>
            	<h2 class="center">Not Found</h2>
		<p class="center">Sorry, but you are looking for something that isn't here.</p>
		<?php get_search_form(); ?>
	<?php endif; ?>
        </div>
<?php get_sidebar(); ?>       
        </div>
<?php get_footer(); ?>