Show posts assigned to multiple categories in current category page

Question

Whenever I assign a post to multiple categories, the WordPress website just loads posts from Cat1 only. No error message.

My post-assignment method:

Post1 - Cat1
Post2 - Cat2
Post3 - Cat1,Cat3,Cat4
Post4 - Cat3
Post5 - Cat4

Here’s my code that works if each post is assigned to one single category. Once a post is assigned to multiple categories, it fails. This is the code I have on category.php

<?php
    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    $myposts = get_posts(
        array(
            'numberposts' => -1, 
            'offset' => 0, 
            'category__in' => array($category),
            'post_status'=>'publish',
            'order'=>'ASC' 
            )
        );
    foreach($myposts as $post) : setup_postdata($post);
?>
<a href="<?php the_permalink() ?>"><?php echo substr(get_the_title(),0,55); ?></a>
<?php endforeach; ?>
<?php wp_reset_query(); ?>

Where am I going wrong? I don’t know how to fix this.

0
user3362364 2 weeks 2023-11-19T05:20:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse