iBegins
 

Code lấy một giá trị category từng bài viết WordPress

Bạn gắn bất kể đâu nơi hiển thị bài viết bằng code php sau: 

Chú ý : bạn phải cài Custim Fields ACF Plugin mới dùng cái này, còn bạn không dùng plugin đó thì cho đoạn code dưới.

Có cài plugin đó bạn nhớ cho 1 field images vào category

<?php
$category = get_the_terms( get_the_ID(), 'category' );
$draught_links = array();
foreach ( $category as $term ) {
$draught_links[] = $term->term_id;
}
$id_category = join( ", ", $draught_links );
$imgnews = get_field('images', 'category_'.$id_category); 
foreach($category as $term) {
if ($imgnews ){?>
<p>
<a href="/category/<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>" target="_blank"><img src="<?php echo $imgnews;?>" alt="<?php echo $term->name; ?>" width="28" height="28"></a>
</p>
<p><a href="/category/<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>" target="_blank"><?php echo $term->name; ?></a></p>
<?php } } ?>

Code không cần plugin:

<?php
$category = get_the_terms( get_the_ID(), 'category' );
foreach($category as $term) {?>
<p>
<a href="/category/<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>" target="_blank"><img src="<?php echo $imgnews;?>" alt="<?php echo $term->name; ?>" width="28" height="28"></a>
</p>
<p><a href="/category/<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>" target="_blank"><?php echo $term->name; ?></a></p>
<?php } ?>

Demo Ảnh

Ngày 1 Tháng Ba, 2018 ( 596 )

Tagged with: , , ,

Bình luận bài viết

Đăng nhập để bình luận và xem các bình luận khác.