Shortcode for related posts
-
[ Moved to the Fixing WordPress sub-forum. ]
Hello,
I’m using bellow function for related post shortcode and I have two question about it, first: it’s supposed to display posts with related tag but it doesn’t do that what’s wrong?. Second: how the code should be changed so I can use something like[rps tag=google]
and the function just return a post has a tag as “google”?
This is the code:add_shortcode('rps', 'fphp_get_related_posts'); function fphp_get_related_posts() { $reset_post = $post; global $post; $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { $post_tag_ids = array(); foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id; $args=array( 'tag__in' => $post_tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page' => 1, 'orderby' => 'rand', ); $related_query = new wp_query( $args ); if (intval($related_query->post_count) === 0) return ''; $html = '<div class="rps"><ul><h3>همچنین بخوانید</h3>'; $related_query->the_post(); $html .= '<li style="width:250px">'; $html .= '<div class="relatedthumb"><a rel="external" href="'. get_the_permalink(). '">'; $html .= get_the_title() . '</a>'; $html .= '</div></li>'; } $post = $reset_post; wp_reset_query(); $html .= '</ul></div>'; return $html; }
- The topic ‘Shortcode for related posts’ is closed to new replies.