为什么分页在Category.php上不起作用

时间:2011-05-05 作者:tobrother

有两个不同的类别“生命科学”和“普通实验室”,还有关于“生命科学”的子类别产品。现在“普通实验室”类别想要获取“生命科学”拥有的所有子类别和产品。代码在类别中。php。以下是全部代码:<代码>

    <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 个回复
SO网友:Jan Fabry

当你转到“通用实验室”的第二页时,WordPress会查询该类别的帖子,但没有找到。如果WordPress没有找到帖子,它将显示404模板。唯一的情况是when you are on the first page of a taxonomy (像一个类别):然后它仍然加载特定的模板,允许您显示类似“此类别中没有帖子”的消息。我在an answer to a similar question.

因此,您不应该在模板中进行查询,因为这样做太晚了。相反,钩住pre_get_posts 措施:

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\';
    }
}
现在它将加载life-sciences 带有主查询的POST和分页将起作用。模板中不需要额外的查询。您可以通过读取全局$wpse16387_original_category 变量

SO网友:nguyen van quan

请删除代码:

<p style="text-decoration: line-through;">$paged = (get_query_var(\'paged\') ? get_query_var(\'paged\') : 1);</p>
使用此选项:

$the_query = new WP_Query("posts_per_page=10&category_name=life-sciences&paged=".**(get_query_var(\'paged\') ? get_query_var(\'paged\') : 1)**); ?>

结束

相关推荐

Broken category pagination

出于某种原因,下面的代码一直工作到我到达第三页并给出404。不使用任何插件就可以修复它吗?<?php $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(\'cat=\'. $id .\'&posts_per_page=4&paged=\'.$paged); while ($wp_query->have_posts()) : $wp_query