0

I've got a problem with a custom category template. Used on sub-categories for a particular parent.

For some odd reason it's not showing posts store within it, but instead from a another sub-category.

I don't quite understand it, as the loop is the same as is used on my category.php page. The difference if the code before hand.

Here is the code:

<?php get_header(); ?>


<?php global $post; ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<div class="parallax" id="parallax1" style="background: url(<?php echo $src[0]; ?> ) !important; background-position: 50% 50% !important; background-repeat: no-repeat !important; background-size: cover; background-attachment: fixed !important;">
    <div class="parallax-content">
        <h1 class="page-title"><?php
                printf( __( '%s', 'adventure' ), '<span>' . single_cat_title( '', false ) . '</span>' );
        ?></h1>
        <?php
            // Display optional category description
            $category_description = category_description();
            if ( ! empty( $category_description ) )
            echo '<div class="archive-meta">' . $category_description . '</div>';
            get_template_part( 'loop', 'category' );            
        ?>
    </div>
</div>

<div id="page-wrapper">

<div id="content-container">
  <div class="page-content">
        <!-- START THE LOOP -->
        <?php $posts=query_posts($query_string . '&order=asc');
while (have_posts()) : the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

        <?php echo catch_video() ?>

            <!-- Get featured image -->
                    <?php if ( has_post_thumbnail() ) : ?>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_post_thumbnail(); ?>
                    </a>
            <?php endif; ?>
            <div class="post-content">

                <div class="post-info top">
                    <span class="post-date"><?php the_date('j F, Y'); ?></span>
                    <span class="no-caps post-autor">by <?php the_author(); ?></span>
                </div>
                <h1 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                <?php print_excerpt(500); ?>
                <a class="read-more-btn" href="<?php the_permalink(); ?>"><?php _e( 'Read More &rarr;'); ?></a>
                <div class="post-info bottom">
                    <span class="post-date">In: <?php the_category(', ') ?>&nbsp;    <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> </a>
</span>
                </div>
                <div class="clear"></div>

            </div><!-- post content -->
        </article>
    <?php endwhile; ?>
    <div id="blog-pagination">
        <div class="alignleft"><?php previous_posts_link('«&nbsp; Previous'); ?></div>
        <div class="alignright"><?php next_posts_link('Next &nbsp; »'); ?></div>
    </div>
  </div><!-- page content -->


</div><!-- content container -->
</div><!-- page wrapper -->

<?php get_footer(); ?>

Can anyone help me understand what is happening here. I thought it might be because there is some kind of pseudo loop used to get the category titles and description, but I tired resetting the query 'wp_reset_query();' without any success.

Thanks

  • what's inside the file loop-category.php? also note that you should never use query_posts. see pre_get_posts for changing the sort order on your category pages. – Milo Feb 26 '15 at 23:04
  • I don't have a loop-category.php never seen one in a theme before. There's a category.php, but I've not got it active at the moment, so if WP doesn't find a template with will use index.php. – C-M Feb 27 '15 at 8:37
  • Right, so it looks like query_posts is the problem as that upsets the loop. Remove that and it all works as normal. Now I just need to get my posts for that page in ASC order. – C-M Feb 27 '15 at 11:30
  • You should never under any circumstances use query_posts, use WP_Query or the pre_get_posts filter instead – Tom J Nowell Feb 27 '15 at 16:31
1

Remove query_posts and add a pre_get_posts action to your functions.php file to modify sort order:

function wpd_category_sort_order( $query ) {
    if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpd_category_sort_order' );
| improve this answer | |
  • The thing is if I do that it's going to change it for every category. I only want it on sub-categories of category 67. – C-M Feb 28 '15 at 12:23

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.