Thursday, April 25, 2024
HomeThiết kế WebWeb Source CodeHiển thị bài viết liên quan mà không cần dùng plugin trong...

Hiển thị bài viết liên quan mà không cần dùng plugin trong wordpress

Nếu bạn nào không muốn dùng plugin có sẵn trên mạng thì mình xin chia sẻ đoạn code làm điều này , các bạn chỉ cần paste đoạn code này vào file single.php trong folder giao diện của các bạn:

[code=’php’]
ID);
if ($tags) {
echo ‘Related Posts’;
$first_tag = $tags[0]->term_id;
$args=array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
‘showposts’=>5, //Display this number of related posts
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

‘;
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(‘,’, $tagsarray);
// Do the query
$q = “SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy =’post_tag’ AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = ‘publish’
AND p.post_date_gmt < NOW() GROUP BY tr.object_id ORDER BY count DESC, p.post_date_gmt DESC LIMIT $limit;"; $related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= ‘

  • RELATED ARTICLES

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    - Advertisment -

    Most Popular