0

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!

0

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>';
1
  • It doesn't work unfortunately =( – Luca Aug 25 '20 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.