TransWikia.com

Showing related post for custom taxonomy

WordPress Development Asked by Luca on November 28, 2020

I need to edit single.php file in order to show related post for a custom taxonomy I’ve already created (named ‘tag-italia).

Can anyone help me to achieve that?

For the "general" related post by tag I’m using this code, which shows the related post only if there’s something to show:

<div>
<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {

echo '<div><h3 class="titolo-related">Potrebbe interessarti:'; the_tags(' #', $sep = ' | #'); echo'</h3>'?>

<?php while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div>
<div><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div>
</br><h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt() ?>
</div>
</div>

I’ve tried to customize this code without any success.

Thanks in advance!

One Answer

Try this

  $tags = wp_get_post_tags($post->ID);
  $tag_ids = [];
  foreach($tags as $individual_tag){
     $tag_ids[] = $individual_tag->term_id;
   }

   $args=array(
      'tag__in' => $tag_ids,
      'post__not_in' => array($post->ID),
      'posts_per_page'=> 4,
    );
    $my_query = new WP_Query( $args );
    
   //Check
    echo '<pre>';
    print_r($my_query);
    echo '</pre>';

Answered by Sobir on November 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP