0

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.

1 Answer 1

0

That's because that's what you're asking for:

'category__in' => array($category),

It's only going to show posts in that category.

You shouldn't be using get_posts() or WP_Query() in category.php. WordPress has already queried the correct posts. You should only be using the standard loop to display them:

<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // Display post content
    endwhile;
endif;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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