0

For category.php my custom query code isn't displaying the posts from the correct category.

I have a parent category called 'Blog' and it can have any number of unknown children. With that in mind I'm trying to automate my loop so it can detect the category and display just the articles for that category (even though the name of which is unknown).

<section id="posts">
    <div class="inner">

        <?php
        global $paged;
        global $post;
        $do_not_duplicate = array();
        $categories = get_the_category();
        $category = $categories[0];
        $cat_ID = $category->cat_ID;
        $myposts = get_posts('category='.$cat_ID.'&paged='.$paged);
        $do_not_duplicate[] = $post->ID;
        $c = 0;
        ?>

        <?php foreach($myposts as $post) : setup_postdata($post); $c++;
            if( $c % 2 == 0) {
                $style = 'even animated fadeIn';
                $c = 0;
            }
            else $style='animated fadeIn'; ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class($style) ?>>
                    <a class="article-block" href="<?php the_permalink(); ?>">
                    <?php if ( has_post_thumbnail() ) { ?>
                    <div class="c-2 post-image">
                        <span class="cat"><?php $category = get_the_category(); echo $category[1]->cat_name;?></span>
                        <span class="overlay">
                            <div class="outer">
                                <div class="middle">
                                    <div class="c-inner">
                                        <span class="button">Read more</span>
                                    </div>
                                </div>
                            </div>
                        </span>

                        <?php the_post_thumbnail('full'); ?>

                    </div>
                    <?php } ?>
                    <div class="c-2 snippet">
                        <h2><?php the_title(); ?></h2>
                        <?php the_content('Read more'); ?>
                    </div>
                    </a>
            </article>

        <?php endforeach; ?>

        <div class="load-more-wrap">
            <div id="wait">Loading Posts...</div>
            <button class="load-more button">Load More</button>
         </div> 
    </div>
</section>
2
  • 1
    why do you query for posts in category.php and not just use the default query/loop that WordPress generates automatically?
    – Milo
    May 14 '13 at 16:25
  • @Milo 2 points 1) What do you mean by automatically? I'd love a quick / easy way to achieve this. 2) To answer your question, I have a load more posts button that I'd like to use on the category.php page, see updated question for full code i'm using.
    – egr103
    May 14 '13 at 17:06
0

in your code, you are checking the category of a post which is quite random outside of the loop and when the posts have more than one category;

the category ID of a category archive page is usually:

$cat_ID = get_query_var('cat');

0

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.