Mostrar entradas relacionadas por categoría en WordPress

  • Autor Autor Jblogger
  • Fecha de inicio Fecha de inicio
Jblogger

Jblogger

1
Épsilon
Redactor
Verificación en dos pasos activada
Suscripción a IA
Googleando vi este código para poner entradas relacionadas sin plugin:

Insertar CODE, HTML o PHP:
<!-- Entradas relacionadas -->
<?php
//para poner en el loop, muestra 5 titulos de post relacionados con la primera tag del post actual
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo '<h3>Artículos relacionados:</h3>';
  $first_tag = $tags[0]->term_id;
  $args=array(
	'tag__in' => array($first_tag),
	'post__not_in' => array($post->ID),
	'showposts'=>5,
	'caller_get_posts'=>1
   );
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) {
	while ($my_query->have_posts()) : $my_query->the_post(); ?>
	  <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace permanente a <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
	  <?php
	endwhile;
  }
}
?>
<!-- Fin Entradas relacionadas -->

El problema de este código es que busca entradas relacionadas sólo basándose en la primera tag o etiquetas. Yo quiero que ofrezca entradas relacionadas por categorías. No he encontrado nada así por los buscadores.

¿Alguien me echa un cable? ¡Gracias!
 
HTML:
<?php query_posts('cat=X&showposts=10'); ?>

  <?php while (have_posts()) : the_post(); ?>
    <!-- Do special_cat stuff... -->
  <?php endwhile;?>

Estoy tratando de mejorarlo....(el código lo leí a2knieto)

Espero que de algo te sirva, saludos!
 
La verdad es que no tengo ni idea de php. Te agradezo la ayuda pero, ¿qué hago con ese código? ¿Cuál sería el código final que debería poner?
 
Última edición:
Descarto la posibilidad de emplear cualquier plugin. Por favor necesito ayuda para hacer entradas relacionadas por categorías.
 
Mira, te dejo este código que muestra las entradas relacionadas por categoría con su respectivo thumbnail.

Tu puedes buscar la forma de quitar el thumbnail y dejar sólo los enlaces. ¿Para qué ser tan malo si podemos quitarle las imágenes borrando una línea?

Este está con thumbnails
PHP:
    <?php $orig_post = $post;
    global $post;
    $categories = get_the_category($post->ID);
    if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

    $args=array(
    'category__in' => $category_ids,
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 2, // Número de posts que se mostrarán.
    'caller_get_posts'=>1
    );

    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {
    echo '<div id="related_posts"><h3>Posts Relacionados</h3><ul>';
    while( $my_query->have_posts() ) {
    $my_query->the_post();?>

    <li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
    <div class="relatedcontent">
    <h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_time('M j, Y') ?>
    </div>
    </li>
    <?
    }
    echo '</ul></div>';
    }
    }
    $post = $orig_post;
    wp_reset_query(); ?>

Este sólo con enlaces
PHP:
    <?php $orig_post = $post;
    global $post;
    $categories = get_the_category($post->ID);
    if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

    $args=array(
    'category__in' => $category_ids,
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 2, // Número de posts que se mostrarán.
    'caller_get_posts'=>1
    );

    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {
    echo '<div id="related_posts"><h3>Posts Relacionados</h3><ul>';
    while( $my_query->have_posts() ) {
    $my_query->the_post();?>

    <li>
    <div class="relatedcontent">
    <h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_time('M j, Y') ?>
    </div>
    </li>
    <?
    }
    echo '</ul></div>';
    }
    }
    $post = $orig_post;
    wp_reset_query(); ?>

También se muestra la fecha del post, pero la puedes quitar eliminando la línea:
PHP:
<?php the_time('M j, Y') ?>

Lo encontré en WPeginner

Espero que te sirva :ciao:
 
Última edición:
Gracias Juank, te has ganado el cielo. Ahora probaré, te estoy muy agradecido.

Un saludo
 
Atrás
Arriba