Thêm đoạn code bên dưới vào file function.php
function lbk_ads_and_post($category = 0) {
if ( is_single() ) $post_id = get_the_ID();
$args = array(
'post_status' => 'publish',
'numberposts' => 5,
'exclude' => array($post_id),
'orderby' => 'rand'
);
if ($category) $args['category'] = array($category);
$recentPosts = get_posts( $args );
ob_start();
?>
<div class="row">
<div class="col large-6 medium-6 small-12">
<div class="col-inner">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20async%20src%3D%22https%3A%2F%2Fpagead2.googlesyndication.com%2Fpagead%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-2585509486854194"
data-ad-slot="7040955534"></ins>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%20%20%20%20(adsbygoogle%20%3D%20window.adsbygoogle%20%7C%7C%20%5B%5D).push(%7B%7D)%3B%0A%20%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
</div>
</div>
<div class="col large-6 medium-6 small-12">
<div class="col-inner">
<div class="relatedcat random-post">
<?php if ( $recentPosts ) : ?>
<h3 class="title-bl">Xem nhiều tuần qua:</h3>
<ul>
<?php foreach ($recentPosts as $post) : ?>
<li>
<a href="<?php echo the_permalink($post->ID); ?>" title="<?php echo $post->post_title; ?>">
<?php echo $post->post_title; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif ?>
</div>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
Cách sử dụng
Dán đoạn code bên dưới vào vị trí cần hiển thị trong file content-single.php
<?php echo lbk_ads_and_post(get_the_category()[0]->term_id); ?>
Thêm style vào file style.css để title hiển thị 1 dòng
.random-post a {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
line-clamp: 1;
-webkit-box-orient: vertical;
}
.random-post>ul>li {
margin-left: 0;
}
