Tin tức liên quan, related news tags (ok)

<?php
      $tags = get_the_tags();
      if (isset($tags) && count($tags[0]->term_id) >0) {
        echo '<div class="relatednewswrap">';
          echo '<p class="relatednews">Tin liên quan</p>';
          $tagall = [];
          $countarr =  count($tags);
          foreach ($tags as $key => $value) {
            $tagall[] = $tags[$key]->term_id;
          }
          $args=array(
            'tag__in' => $tagall,
            'post__not_in' => array($post->ID),
            'posts_per_page'=>5,
            'caller_get_posts'=>1
          );
          ?>
          <div uk-slider>
            <div class="uk-position-relative">
              <div class="uk-slider-container uk-light">
                <div class="detailwrapout uk-slider-items uk-child-width-1-2 uk-child-width-1-3@s uk-child-width-1-3@m">
                  <?php
                  $my_query = new WP_Query($args);
                  if( $my_query->have_posts() ) {
                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <div class="detailwrapin">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                        <div class="contentdetail">
                          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                          <div class="date"><?php $post_date = get_the_date( 'l F j, Y' );  echo $post_date; ?></div>
                          <div class="excerpt"><?php echo the_excerpt(); ?></div>
                        </div>
                    </div>
                  <?php
                  endwhile;
                  }
                  wp_reset_query();
                  ?>
                </div>
              </div>
              <div class="uk-visible@s">
                <a class="relatednews uk-position-center-left-out uk-position-small" href="#" uk-slidenav-previous uk-slider-item="previous"></a>
                <a class="relatednews uk-position-center-right-out uk-position-small" href="#" uk-slidenav-next uk-slider-item="next"></a>
              </div>
            </div>
          </div>
        </div>
      <?php
      }
    ?>

Hoặc

<?php
/**
 * The template for displaying all single posts.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 */
namespace YOOtheme;
get_header();
setPostViews(get_the_ID());
$config = app(Config::class);
if (have_posts()):
  $attrs_container = [];
  if ($content_width = $config('~theme.post.content_width')) {
    $attrs_container['class'][] = "uk-container uk-container-{$content_width}";
  }
  while (have_posts()): the_post();
    get_template_part('templates/post/content', get_post_format());
    if ($attrs_container):
      printf('<div%s>', get_attrs($attrs_container));
    endif;
    if (comments_open() || get_comments_number()):
      comments_template();
    endif;
    if ($attrs_container):
      echo '</div>';
    endif;
  endwhile;
?>
<?php
  $tags = get_the_tags();
  global $post;
  $custom_posts = get_posts($args);
  if (isset($tags) && ($tags[0]->term_id >0) ) {
      echo '<div class="relatednewswrap">';
        echo '<p class="relatednews">Tin liên quan</p>';
        $tagall = [];
        $countarr =  count($tags);
        foreach ($tags as $key => $value) {
          $tagall[] = $tags[$key]->term_id;
        }
        $args = array(
          'tag__in' => $tagall,
          'post__not_in' => array($post->ID),
          'posts_per_page'=>5
        );
        $custom_posts = get_posts($args);
        ?>
        <div uk-slider>
            <div class="uk-position-relative">
              <div class="uk-slider-container uk-light">
                <div class="detailwrapout uk-slider-items uk-child-width-1-2 uk-child-width-1-3@s uk-child-width-1-3@m">
                  <?php
                  foreach($custom_posts as $post) : setup_postdata($post);
                  ?>
                    <div class="detailwrapin">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                        <div class="contentdetail">
                          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        </div>
                    </div>
                  <?php
                  endforeach;
                  ?>
                </div>
              </div>
              <div class="uk-visible@s">
              <a class="relatednews uk-position-center-left-out uk-position-small" href="#" uk-slidenav-previous uk-slider-item="previous"></a>
              <a class="relatednews uk-position-center-right-out uk-position-small" href="#" uk-slidenav-next uk-slider-item="next"></a>
            </div>
            </div>
        </div>
        <?php
          wp_reset_postdata();
        ?>
      </div>
    <?php
    }
  endif;
get_footer();

Last updated