$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Related Posts: if there is no category use tags|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far