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();
}
}
?>
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; } }" ?
– haRdy