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');
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.