php - Related Posts: if there is no category use tags

admin2025-06-04  0

I found this script on one of the sites. This script is in accordance with what I want. Related articles use category posts, if there are no categories, they will take tags from posts.

But this script doesn't work properly, can you help me fix it?

function dtwd_related_posts_shortcode($atts){
extract(shortcode_atts(array(
'count'=> '3',
'title' => 'More useful tips',
), $atts));
global $post;
$current_cat = get_the_category($post->ID);
$current_cat = $current_cat[0]->cat_ID;
$this_cat = ";
$tag_ids = array();
$tags = get_the_tags($post->ID);
if ($tags) {
foreach($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
} else {
$this_cat = $current_cat;
}
$args = array(
'post_type' => get_post_type(),
'numberposts' => $count,
'orderby’ => 'date',
'order' => 'DESC',
'tag__in' => $tag_ids,
'cat' => $this_cat,
'exclude' => $post->ID
);
$dtwd_related_posts = get_posts($args);
if ( empty($dtwd_related_posts) ) {
$args['tag__in'] = ";
$args['cat'] = $current_cat;
$dtwd_related_posts = get_posts($args);
}
if ( empty($dtwd_related_posts) ) {
return;
}
$post_list = ";
foreach($dtwd_related_posts as $dtwd_related) {
$post_list .= '<li><a href="' . get_permalink($dtwd_related->ID) . '">' . $dtwd_related->post_title . '</a></li>';
}
return sprintf('
<div class="dtwd_related-posts">
<h4>%s</h4>
<ul>%s</ul>
</div> <!- .dtwd_related-posts ->
', $title, $post_list );
}
add_shortcode('dtwd_related_posts_sc', 'dtwd_related_posts_shortcode');

I found this script on one of the sites. This script is in accordance with what I want. Related articles use category posts, if there are no categories, they will take tags from posts.

But this script doesn't work properly, can you help me fix it?

function dtwd_related_posts_shortcode($atts){
extract(shortcode_atts(array(
'count'=> '3',
'title' => 'More useful tips',
), $atts));
global $post;
$current_cat = get_the_category($post->ID);
$current_cat = $current_cat[0]->cat_ID;
$this_cat = ";
$tag_ids = array();
$tags = get_the_tags($post->ID);
if ($tags) {
foreach($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
} else {
$this_cat = $current_cat;
}
$args = array(
'post_type' => get_post_type(),
'numberposts' => $count,
'orderby’ => 'date',
'order' => 'DESC',
'tag__in' => $tag_ids,
'cat' => $this_cat,
'exclude' => $post->ID
);
$dtwd_related_posts = get_posts($args);
if ( empty($dtwd_related_posts) ) {
$args['tag__in'] = ";
$args['cat'] = $current_cat;
$dtwd_related_posts = get_posts($args);
}
if ( empty($dtwd_related_posts) ) {
return;
}
$post_list = ";
foreach($dtwd_related_posts as $dtwd_related) {
$post_list .= '<li><a href="' . get_permalink($dtwd_related->ID) . '">' . $dtwd_related->post_title . '</a></li>';
}
return sprintf('
<div class="dtwd_related-posts">
<h4>%s</h4>
<ul>%s</ul>
</div> <!- .dtwd_related-posts ->
', $title, $post_list );
}
add_shortcode('dtwd_related_posts_sc', 'dtwd_related_posts_shortcode');
Share Improve this question edited Jan 22, 2019 at 1:37 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Jan 22, 2019 at 1:19 R.M. RezaR.M. Reza 1417 bronze badges 2
  • Hello! Can you indent your code? It's very difficult to read – Tom J Nowell Commented Jan 22, 2019 at 1:36
  • thank you for fixing it. can you help me @TomJNowell , the code can't be used. – R.M. Reza Commented Jan 22, 2019 at 2:09
Add a comment  | 

1 Answer 1

Reset to default 0

There is no mistake in code it is not working because of just comma mistake use this i have made charges into it.

function dtwd_related_posts_shortcode($atts){
        extract(shortcode_atts(array(
            'count'=> '3',
            'title' => 'More useful tips',
        ), $atts));
        global $post;
        $current_cat = get_the_category($post->ID);
        $current_cat = $current_cat[0]->cat_ID;
        $this_cat = '';
        $tag_ids = array();
        $tags = get_the_tags($post->ID);
        if ($tags) {
            foreach($tags as $tag) {
                $tag_ids[] = $tag->term_id;
            }
        } 
        else {
            $this_cat = $current_cat;
        }
        $args = array(
            'post_type' => get_post_type(),
            'posts_per_page' => $count,
            'orderby' => date,
            'order' => 'DESC',
            'tag__in' => $tag_ids,
            'cat' => $this_cat,
            'exclude' => $post->ID
        );
        $dtwd_related_posts = get_posts($args);
        if ( empty($dtwd_related_posts) ) {
            $args['tag__in'] = '';
            $args['cat'] = $current_cat;
            $dtwd_related_posts = get_posts($args);
            }
            if ( empty($dtwd_related_posts) ) {
            return;
        }
        $post_list = '';
        foreach($dtwd_related_posts as $dtwd_related) {
        $post_list .= '<li><a href="' . get_permalink($dtwd_related->ID) . '">' . $dtwd_related->post_title . '</a></li>';
        }
        return sprintf('
        <div class="dtwd_related-posts">
        <h4>%s</h4>
        <ul>%s</ul>
        </div> <!- .dtwd_related-posts ->
        ', $title, $post_list );
}
add_shortcode('dtwd_related_posts_sc', 'dtwd_related_posts_shortcode');

Hope this help.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748987973a315414.html

最新回复(0)