0

There are two different category "Life Sciences" and "General Lab" and there are sub-categorical product on "Life Sciences". Now "General Lab" category want to fetch all the sub-category and products which "Life sciences" have. The code is in category.php. Here is the whole code:

    <div id="container">
        <div id="content" role="main">

        <?php if ( is_category('general-lab') ) : ?>

            <?php //FETCHING ONLY GENERAL LAB CATEGORY

                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

                $the_query = new WP_Query("posts_per_page=10&category_name=life-sciences&paged=".$paged); ?>

                <h1 class="entry-title"><?php echo single_cat_title("", TRUE); ?></h1>

            <?php $count=0; while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

            <?php if($count % 2 == 0) echo '<div class="left">'; else echo '<div class="right">'; ?>
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="cat-thumb"><?php echo get_post_meta($post->ID, '_mcf_block-one', true); ?></div>
                    <div class="cat-entry">
                    <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>     
                    <?php the_excerpt(); ?>
                    </div>
                </div>
            </div>  
            <?php if($count % 2 != 0) echo '<div class="clear"></div>';?>

            <?php $count++; endwhile; ?>

            <?php if (  $the_query->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php previous_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'twentyten' ) ); ?></div>
                <div class="nav-next"><?php next_posts_link( __( 'Next <span class="meta-nav">&rarr;</span> ', 'twentyten' ) ); ?></div>
            </div><!-- #nav-below -->
            <?php endif; ?>

        <?php else : ?>

        <h1 class="entry-title"><?php echo single_cat_title("", TRUE); ?></h1>
        <?php $count=0; while (have_posts()) : the_post(); ?>

        <?php if($count % 2 == 0) echo '<div class="left">'; else echo '<div class="right">'; ?>
            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <div class="cat-thumb"><?php echo get_post_meta($post->ID, '_mcf_block-one', true); ?></div>
                <div class="cat-entry">
                <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>     
                <?php the_excerpt(); ?>
                </div>
            </div>
        </div>  
        <?php if($count % 2 != 0) echo '<div class="clear"></div>';?>

        <?php $count++; endwhile; ?>
        <?php if (  $wp_query->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php previous_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'twentyten' ) ); ?></div>
                <div class="nav-next"><?php next_posts_link( __( 'Next <span class="meta-nav">&rarr;</span> ', 'twentyten' ) ); ?></div>
            </div><!-- #nav-below -->
        <?php endif; ?>

        <?php endif; ?>
        </div><!-- #content -->

    </div><!-- #container -->   

<?php $current_category = single_cat_title("", FALSE);
            $parent_cat = get_the_category();
            $back_to_current = get_cat_name($parent_cat[0]->category_parent);

if ( is_category( array( 'life-sciences','consumables','histology','forensics','pharmaceutical' ) ) == $current_category) { ?>
<?php echo '<div id="cat-menu"><h3>'.$back_to_current.'</h3>'; wp_nav_menu( array('container_id' => 'sub-page', 'menu' => $current_category ) ); echo '</div>';  } elseif ( is_category('general-lab')) { ?>
<?php echo '<div id="cat-menu"><h3>'.$current_category.'</h3>'; wp_nav_menu( array('container_id' => 'sub-page', 'menu' => $back_to_current ) ); echo '</div>'; } else { ?>
<?php echo '<div id="cat-menu"><h3>'.$back_to_current.'</h3>'; wp_nav_menu( array('container_id' => 'sub-page', 'menu' => $back_to_current ) ); echo '</div>'; } ?>

2

2 Answers 2

1

When you go to the second page of the "General Lab", WordPress does a query for posts in that category and finds none. If WordPress finds no posts, it will display the 404 template. The only case is when you are on the first page of a taxonomy (like a category): then it still loads the specific template, allowing you to display a message like "No posts in this category". I explained this further in an answer to a similar question.

You should thus not do the query in the template, since it will be too late. Instead, hook into the pre_get_posts action:

add_action( 'pre_get_posts', 'wpse16387_pre_get_posts' );
function wpse16387_pre_get_posts( &$wp_query )
{
    if ( 'general-lab' == $wp_query->get( 'category' ) ) {
        $wp_query->set( 'category', 'life-sciences' );
        $GLOBALS['wpse16387_original_category'] = 'general-lab';
    }
}

Now it will load the life-sciences posts with the main query, and paging will work. You don't need the extra query in your template. You can detect you swapped the categories by reading the global $wpse16387_original_category variable.

-2

Please remove code:

<p style="text-decoration: line-through;">$paged = (get_query_var('paged') ? get_query_var('paged') : 1);</p>

Use this one:

$the_query = new WP_Query("posts_per_page=10&category_name=life-sciences&paged=".**(get_query_var('paged') ? get_query_var('paged') : 1)**); ?>
2
  • 3
    Please consult the help center and see how to answering works. Please also use the WYSIWYG editor buttons to format your content.
    – kaiser
    Dec 2, 2014 at 2:00
  • Never replace the main query with a custom one, it is always the cause of other problems. Use pre_get_posts to alter the main query rather Dec 2, 2014 at 6:12

Your Answer

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

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