Mostrar Publicaciones Relacionadas por Etiquetas fácilmente

  • Autor Autor eic2010
  • Fecha de inicio Fecha de inicio
E

eic2010

Beta
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Hola como están? quisiera saber como puedo mostrar Posts Relacionados por los Tags? a continuación le muestro los que tengo ( porque a alguien le puede servir), posts relacionado por categoría y posts aleatorios, pero me falta por tags :ambivalence:


PHP:
<div class='sigue'> POST RELACIONADOS POR CATEGORIA</div>

<?php
		  
  $rand_posts = get_posts('category_name="MICATEGORIA"&showposts=2');
  foreach( $rand_posts as $post ) : ?>
 
 <div class="article-loop" >
         
            <?php     if ( has_post_thumbnail() ) { ?> 
        <div class='post-thumb'> 
                <a href="<?php the_permalink();?>" > 
               <?php the_post_thumbnail(''); ?> 
                </a> 
        </div> 
           <?php the_category(); ?>
          <h7><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h7>       
         
    <?php } ?> 
      </div> 
	
<?php endforeach; ?>  
	 	  		  
		  
	<div class='sigue'> POST ALEATORIOS</div>

<?php
		  
  $rand_posts = get_posts('numberposts=4&orderby=rand');
  foreach( $rand_posts as $post ) : ?>
 
 <div class="article-loop" >
         
            <?php     if ( has_post_thumbnail() ) { ?> 
        <div class='post-thumb'> 
                <a href="<?php the_permalink();?>" > 
               <?php the_post_thumbnail(''); ?> 
                </a> 
        </div> 
           <?php the_category(); ?>
          <h7><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h7>       
         
    <?php } ?> 
      </div> 
	
<?php endforeach; ?>


La idea es que el visitante tenga mas opciones para seguir navegando es nuestras webs :topsy_turvy:
 
Hola como están? quisiera saber como puedo mostrar Posts Relacionados por los Tags? a continuación le muestro los que tengo ( porque a alguien le puede servir), posts relacionado por categoría y posts aleatorios, pero me falta por tags :ambivalence:


PHP:
<div class='sigue'> POST RELACIONADOS POR CATEGORIA</div>

<?php
         
  $rand_posts = get_posts('category_name="MICATEGORIA"&showposts=2');
  foreach( $rand_posts as $post ) : ?>
 
 <div class="article-loop" >
        
            <?php     if ( has_post_thumbnail() ) { ?>
        <div class='post-thumb'>
                <a href="<?php the_permalink();?>" >
               <?php the_post_thumbnail(''); ?>
                </a>
        </div>
           <?php the_category(); ?>
          <h7><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h7>      
        
    <?php } ?>
      </div>
   
<?php endforeach; ?> 
                    
         
    <div class='sigue'> POST ALEATORIOS</div>

<?php
         
  $rand_posts = get_posts('numberposts=4&orderby=rand');
  foreach( $rand_posts as $post ) : ?>
 
 <div class="article-loop" >
        
            <?php     if ( has_post_thumbnail() ) { ?>
        <div class='post-thumb'>
                <a href="<?php the_permalink();?>" >
               <?php the_post_thumbnail(''); ?>
                </a>
        </div>
           <?php the_category(); ?>
          <h7><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h7>      
        
    <?php } ?>
      </div>
   
<?php endforeach; ?>


La idea es que el visitante tenga mas opciones para seguir navegando es nuestras webs :topsy_turvy:
¡Hola! Para mostrar publicaciones relacionadas por etiquetas en WordPress, puedes seguir un enfoque similar al que usaste para las categorías y las publicaciones aleatorias. Aquí te muestro un ejemplo de cómo hacerlo:

PHP:
<div class='sigue'> POSTS RELACIONADOS POR TAGS</div>

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
    $tag_ids = array();
    foreach ($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
   
    $related_args = array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page' => 2,
        'ignore_sticky_posts' => 1
    );

    $related_posts = new WP_Query($related_args);

    if ($related_posts->have_posts()) :
        while ($related_posts->have_posts()) : $related_posts->the_post();
?>

<div class="article-loop">
    <?php if (has_post_thumbnail()) { ?>
        <div class='post-thumb'>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(''); ?>
            </a>
        </div>
        <?php the_category(); ?>
        <h7><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h7>
    <?php } ?>
</div>

<?php
        endwhile;
    endif;
    wp_reset_postdata();
}
?>

En este código, primero obtenemos las etiquetas del post actual y luego usamos esas etiquetas para recuperar publicaciones relacionadas. Las publicaciones relacionadas se muestran de manera similar a las publicaciones por categoría y las publicaciones aleatorias que ya tienes implementadas.

Espero que esta información te sea útil. ¡Dime si necesitas ayuda adicional!
 
Atrás
Arriba