Related Posts by Child Categories

Asked

Viewed 154 times

0

I have the following code working, which shows the related wordpress posts, by category (mother).
I need to adapt this code so that in a particular type of post, it shows related posts by child category. For example

My category structure is like:

  • Mother Category
    • Category Filho1
    • Category Filho2
    • Category Filho3

If the post has the "Mother Category" and "Child Categorie2", it will appear posts related to "Mother Category".

The ideal for the site, is to appear only from the "Category Filho2". And if the post is "Category Mother" and "Category Filho1", appear only "Category Filho1" and etc.

Below follows the code that generates my Related posts.

<?php 
$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), 
    'showposts'=>4,
    'caller_get_posts'=>1 
  ); 
  $my_query = new wp_query($args); 

  if( $my_query->have_posts() ) { 
    echo '<h3 class="related__title title--article-footer">Artigos relacionados:</h3><div class="row">';
    while ($my_query->have_posts()) { 
      $my_query->the_post(); ?>
    <div class="col-xs-12 col-sm-6 col-md-3">
      <a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
        <h2 class="related__title"><?php the_title(); ?></h2>
        <h3 class="related__subtitle"><?php the_excerpt() ?></h3>
      </a>
    </div>
    <?php } 
    echo '</div>'; 
    wp_reset_query();
  } 
} 
?>

1 answer

0


I understood two different things of the question, so follow two answers:

If what you need is to have a specific category that when you are present the related posts will only be hers, use this:

$filha_unica = 20; // o term_id da categoria que você não quer que traga posts da categoria mãe

foreach($categories as $individual_category) {

    // O de sempre aqui, pega o id da categoria para relacionar
    $category_ids[] = $individual_category->term_id;

    // Se apareceu o id da categoria $filha_unica, use apenas ele
    if( $individual_category->term_id === $filha_unica ) {
        $category_ids = array( $filha_unica );
        break;
    }
}

If what you need is that whenever a daughter category is present, do not use the mother category ids, use this:

foreach($categories as $individual_category) {

    // Guarde filhas e mães em arrays separados
    if( $individual_category->category_parent > 0 ) {
        $cat_filhas[] = $individual_category->term_id;
    } else {
        $cat_maes[] = $individual_category->term_id;
    }
}


// Se não houver categorias filhas, use as mães e vice versa
if ( empty( $cat_filhas ) ) {
    $category_ids = $cat_maes;
} else {
    $category_ids = $cat_filhas;
}
  • Trade "foreach($Categories as $individual_category)" by "foreach($Categories as $individual_category) { // Store daughters and mothers in separate arrays if( $individual_category->category_parent > 0 ) { $cat_daughters[] = $individual_category->term_id; } Else { $cat_maes[] = $individual_category->term_id; } }" ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.