/** * Related posts * Displays a list of blog links that are related to this current one using shortcode. * * Usage: [related_posts] * */ function comicpress_related_posts_shortcode($atts = '') { extract(shortcode_atts(array('limit' => '5'), $atts)); global $wpdb, $post, $table_prefix; if ($post->ID) { // Get tags $tags = wp_get_post_tags($post->ID); $tagsarray = array(); foreach ($tags as $tag) { $tagsarray[] = $tag->term_id; } $tagslist = implode(',', $tagsarray); if (empty($tagslist)) { return; } if (empty($limit)) { $limit = 5; } // Do the query $q = "SELECT p.*, count(tr.object_id) as count\n\t\t\t\tFROM {$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 . "\n\t\t\t\tAND p.post_status = 'publish'\n\t\t\t\tAND p.post_date_gmt < NOW()\n\t\t\t\tGROUP BY tr.object_id\n\t\t\t\tORDER BY RAND() DESC, p.post_date_gmt DESC\n\t\t\t\tLIMIT {$limit};"; $related = $wpdb->get_results($q); $retval = ''; $goodtogo = false; if ($related) { $retval = ' <div class="related_posts"> <h4>Related Posts ¬</h4>'; $retval .= ' <ul>'; $in_comic_cat = 0; $counter = 0; $retval .= ' <table class="month-table">'; foreach ($related as $r) { $thecats = array(); $categories = get_the_category($r->ID); $thecats[] = $categories[0]->cat_ID; if (count(array_intersect(comicpress_all_comic_categories_array(), $thecats)) == 0) { $retval .= ' <tr><td class="archive-date" align="right">' . date('M j, Y', strtotime($r->post_date)) . '</td><td class="archive-title"><a title="' . wptexturize($r->post_title) . '" href="' . get_permalink($r->ID) . '">' . wptexturize($r->post_title) . '</a></td></tr>'; $goodtogo = true; } } $retval .= ' </table>'; $retval .= ' </ul>'; $retval .= ' </div>'; } if ($goodtogo) { return $retval; } } return; }
function comicpress_all_comic_categories_string() { global $comicpress_all_comic_categories_string; if (empty($comicpress_all_comic_categories_string)) { $comicpress_all_comic_categories_string = implode(',', comicpress_all_comic_categories_array()); } return $comicpress_all_comic_categories_string; }