Ver Mensaje Individual
  #5 (permalink)  
Antiguo 17/12/2010, 12:51
Avatar de rogertm
rogertm
Mod->Cuba
 
Fecha de Ingreso: julio-2005
Ubicación: /home/Cuba/Habana/rogertm/
Mensajes: 2.922
Antigüedad: 18 años, 9 meses
Puntos: 638
Respuesta: Mostrar post relacionados (en single.php)

Yo uso este código, lo pegas en tu archivo functions.php:
Código PHP:
Ver original
  1. <?php
  2. /* Post Relacionados **********************************************************/
  3. function crossblock_get_related_posts() {
  4.     global $wpdb, $post,$table_prefix;
  5.  
  6.     if ( $exclude != '' ) {
  7.         $q = "SELECT tt.term_id FROM ". $table_prefix ."term_taxonomy tt, " . $table_prefix . "term_relationships tr WHERE tt.taxonomy = 'category' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = $post->ID";
  8.  
  9.         $cats = $wpdb->get_results($q);
  10.  
  11.         foreach(($cats) as $cat) {
  12.             if (in_array($cat->term_id, $exclude) != false){
  13.                 return;
  14.             }
  15.         }
  16.     }
  17.  
  18.     if(!$post->ID){return;}
  19.     $now = current_time('mysql', 1);
  20.     $tags = wp_get_post_tags($post->ID);
  21.  
  22.     $taglist = "'" . $tags[0]->term_id. "'";
  23.  
  24.     $tagcount = count($tags);
  25.     if ($tagcount > 1) {
  26.         for ($i = 1; $i <= $tagcount; $i++) {
  27.             $taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
  28.         }
  29.     }
  30.  
  31.     if ($limit) {
  32.         $limitclause = "LIMIT $limit";
  33.     }    else {
  34.         $limitclause = "LIMIT 9"; // Éste valor es la cantidad de post a mostrar
  35.     }
  36.  
  37.     $q = "SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id  = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";
  38.  
  39.     $related_posts = $wpdb->get_results($q);
  40.     $output = "";
  41.  
  42.     if (!$related_posts){
  43.         return;
  44.     }
  45.  
  46.     foreach ($related_posts as $related_post ){
  47.         $output .= '<li><h3>';
  48.  
  49.         /*
  50.         $show_date = true;
  51.         if ($show_date){
  52.             $dateformat = get_option('date_format');
  53.             $output .= '<span class="date">'.mysql2date($dateformat, $related_post->post_date) . "</span> &nbsp;-&nbsp; ";
  54.         }
  55.         */
  56.  
  57.         $output .=  '<a href="'.get_permalink($related_post->ID).'" title="'.wptexturize($related_post->post_title).'">'.wptexturize($related_post->post_title).'';
  58.  
  59.         if ($show_comments_count){
  60.             $output .=  " (" . $related_post->comment_count . ")";
  61.         }
  62.  
  63.         $output .=  '</h3></a></li>';
  64.     }
  65.     $output = '<ul>'.$output.'</ul>';
  66.     $output = '<h3>Posts relacionados:</h3>'.$output;
  67.  // end $crossblock_postthumbnail_disable
  68.  
  69.     return $output;
  70. }
  71. ?>
Y en single.php solo tienes que llamar la función donde quieras que se muestren esos post relacionados.
Código PHP:
Ver original
  1. <div id="related-post">
  2.     <?php echo crossblock_get_related_posts(); ?>
  3. </div>

Saludos...
__________________
Friki y Blogger por Cuenta Propia:213
Twenty'em: Theming is Prose