Nội dung

Code hiển thị bài viết liên quan cùng tag và chuyên mục trên website wordpress

3 năm trước, Câu hỏi thường gặp, Code theme wordpress, Code website hay dùng, Thủ thuật web, 3209 Lượt xem

Hôm nay netweb.vn xin chia sẻ các bạn code hiển thị bài viết liên quan theo chuyên mục trong website wordpress, để website của các bạn sinh động hơn, và có khả năng tương tác người dùng tốt hơn!

Bài viết có sử dụng thư viện fontawesome các bạn tích hợp vào website ở link đưới ạ!

https://fontawesome.com/v4.7.0/icons/

Code hiển thị bài viết trong cùng 1 chuyên mục (categorys)

Chèn code vài file single.php khi các bạn viết code

<div class="post-chuyen-muc">	
<?php
/*
 * Code hiển thị bài viết liên quan trong cùng 1 chuyên mục
 */
$categories = get_the_category(get_the_ID());
if ($categories){
    $category_ids = array();
    foreach($categories as $netweb_category) $category_ids[] = $netweb_category->term_id;
    $args=array(
        'category__in' => $category_ids,
        'post__not_in' => array(get_the_ID()),
        'posts_per_page' => 2, 
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ):
        echo '<h2>Các bài viết cùng chuyên mục</h3>';
        while ($my_query->have_posts()):$my_query->the_post();
         ?>
			<section class="blog-list">
			<div class="wrap-post">
			<div class="post">									
				<div class="post-image">									
					<a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>">
					  <?php the_post_thumbnail(); ?>
					</a>
				</div>
				<div class="post-info">
					<a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><h3><?php the_title(); ?></h3></a>
					<div class="location"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> <i><?php the_time('d/m/Y'); ?></i>, <i class="fa fa-refresh" aria-hidden="true"></i> <?php the_modified_time('d/m/Y'); ?> <i class="fa fa-clock-o" aria-hidden="true"></i> <?php the_modified_time(); ?> </div>
					<p><?php the_excerpt(); ?> </p>
				</div>
			</div>
			</div>
			</section>
         <?php
        endwhile;
    endif; wp_reset_query();
}
?>	
</div>

Code hiển thị các bài viết cùng tag trong bài viết wordpress

<div class="post-tag">

<?php
/*
 * Code hiển thị bài viết liên quan theo post tag
 */
$tags = wp_get_post_tags(get_the_ID());
if ($tags){

    $tag_ids = array();
    foreach($tags as $netweb_tag) $tag_ids[] = $netweb_tag->term_id;
 
    $args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array(get_the_ID()),
        'posts_per_page' => 3,
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ):
        echo '<h2>Các bài viết theo Tag</h3>';
        while ($my_query->have_posts()):$my_query->the_post();
         ?>
            <section class="blog-list">
			<div class="wrap-post">
			<div class="post">									
				<div class="post-image">									
					<a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>">
					  <?php the_post_thumbnail(); ?>
					</a>
				</div>
				<div class="post-info">
					<a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><h3><?php the_title(); ?></h3></a>
					<div class="location"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> <i><?php the_time('d/m/Y'); ?></i>, <i class="fa fa-refresh" aria-hidden="true"></i> <?php the_modified_time('d/m/Y'); ?> <i class="fa fa-clock-o" aria-hidden="true"></i> <?php the_modified_time(); ?> </div>
					<p><?php the_excerpt(); ?> </p>
				</div>
			</div>
			</div>
			</section>
	
         <?php
        endwhile;

    endif;
    wp_reset_query();
}
?>	

</div>

Chúc các bạn thành công!

Sử dụng với plugin của netweb

Ngoài cách trên các bạn có thể sử dụng plugin áp dụng với những khách hàng của netweb sẽ được tích hợp tính năng này miễn phí, netweb đã viết ra plugin riêng để hỗ trợ tốt nhất cho khách hàng sử dụng dịch vụ web bên mình

Chèn code vào vị trí cần hiển thị và active plugin

[netweb_related_posts_shortcode]
code php
<?php echo do_shortcode('[netweb_related_posts_shortcode]'); ?>
hình xemo bạn có thể xem ngay tại netweb phần bài viết

Bài viết mới cập nhập

  • Hướng dẫn Plugin floating contact

    Hướng dẫn Plugin floating contact

    10 tháng trước, 338 Lượt xem

    Netweb xin hướng dẫn cách quản lý Plugin floating contact các bạn nhớ làm theo kỹ tường bước để plugin hoạt động Tính năng: Hiển…

  • Backup Yandex Mail

    Backup Yandex Mail

    12 tháng trước, 229 Lượt xem

    Khác với dịch vụ email theo tên của microsoft và google với chi phí đắt đỏ tầm 1tr/năm/1user, vì thế nên netweb khuyên dùng dịch…

Bình luận trên facebook netweb.vn

Đam mê thiết kế web, kinh nghiệm 9 năm trong nghề thiết kế website, đang thiết kế web dạo tại netweb.vn, Callme: 08.9898.2526 (Mr Hải)