在Wordpress循环中使用post-count导航

时间:2014-04-13 09:35:14

标签: wordpress navigation

导航不能在我的Wordpress循环中使用PHP条件计数进行后计数。

循环后,

“next_posts”和“previous_posts”不会出现。

这是我的代码。希望你能帮助我,并提前感谢你!

<?php get_header(); ?>
            <!-- Index Layout
            ================================================== -->
            <div id="content" role="main">
                <div class="column twelve" style="height: 430px;">
                    <div class="cover">
                        <img src="<?php bloginfo('template_directory'); ?>/img/home.jpg" alt="" />
                    </div><!-- .cover -->
                </div><!-- .column -->
                <!-- Loop --> 
                <div class="column nine">
                    <?php if (have_posts()) : ?>
                    <?php $count = 0; ?>
                    <?php while (have_posts()) : the_post(); ?>
                    <?php $count++; ?>
                    <?php if ($count == 1) : ?>
                    <div class="column three">
                        <div class="cover">
                            <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                        </div><!-- .cover -->
                    </div><!-- .column -->
                    <div class="column three white">
                        <div class="vertical">
                            <div class="round">
                                <div class="time">
                                    <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                                </div><!-- .time -->
                            </div><!-- .round -->
                                <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                                <div class="category">
                                    <?php the_category(''); ?>
                                </div><!-- .category -->
                        </div><div class="vertical-helper"></div><!-- .vertical -->
                    </div><!-- .column -->
                    <?php elseif($count == 2) : ?>
                    <div class="column three">
                        <div class="cover">
                            <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                        </div><!-- .cover -->
                    </div><!-- .column -->
                    <div class="column three black">
                        <div class="vertical">
                            <div class="round">
                                <div class="time">
                                    <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                                </div><!-- .time -->
                            </div><!-- .round -->
                                <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                                <div class="category">
                                    <?php the_category(''); ?>
                                </div><!-- .category -->
                        </div><div class="vertical-helper"></div><!-- .vertical -->
                    </div><!-- .column -->
                    <?php elseif($count == 3) : ?>
                    <div class="column three">
                        <div class="cover">
                            <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                        </div><!-- .cover -->
                    </div><!-- .column -->
                    <div class="column three black">
                        <div class="vertical">
                            <div class="round">
                                <div class="time">
                                    <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                                </div><!-- .time -->
                            </div><!-- .round -->
                                <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                                <div class="category">
                                    <?php the_category(''); ?>
                                </div><!-- .category -->
                        </div><div class="vertical-helper"></div><!-- .vertical -->
                    </div><!-- .column -->
                </div><!-- .column nine -->
                <div class="column three sidebar">
                    <img src="<?php bloginfo('template_directory'); ?>/img/about.png" alt="" />
                    <h1>MOODYGUY est un magazine en ligne trans* identitaire. </h1>
                        <div class="round">
                            <div class="time">
                                Num<br /><span><?php $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts); ?><?php echo $numposts ?></span>
                            </div><!-- .time -->
                        </div><!-- .round -->
                        <h2>Au sommaire :</h2>
                </div><!-- .column -->
                <?php elseif($count == 4) : ?>
                <div class="column six squarre">
                    <div class="cover">
                        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('squarre'); } else { ?><?php } ?></a>
                    </div><!-- .cover -->
                    <div class="column three white">
                        <div class="vertical">
                            <div class="round">
                                <div class="time">
                                    <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                                </div><!-- .time -->
                            </div><!-- .round -->
                                <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                                <div class="category">
                                    <?php the_category(''); ?>
                                </div><!-- .category -->
                        </div><div class="vertical-helper"></div><!-- .vertical -->
                    </div><!-- .column -->
                </div><!-- .column -->
                <?php elseif($count == 5) : ?>
                <div class="column three">
                    <div class="cover">
                        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                    </div><!-- .cover -->
                </div><!-- .column -->
                <div class="column three white">
                    <div class="vertical">
                        <div class="round">
                            <div class="time">
                                <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                            </div><!-- .time -->
                        </div><!-- .round -->
                            <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                            <div class="category">
                                <?php the_category(''); ?>
                            </div><!-- .category -->
                    </div><div class="vertical-helper"></div><!-- .vertical -->
                </div><!-- .column -->
                <?php elseif($count == 6) : ?>
                <div class="column three black">
                    <div class="vertical">
                        <div class="round">
                            <div class="time">
                                <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                            </div><!-- .time -->
                        </div><!-- .round -->
                            <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                            <div class="category">
                                <?php the_category(''); ?>
                            </div><!-- .category -->
                    </div><div class="vertical-helper"></div><!-- .vertical -->
                </div><!-- .column -->
                <div class="column three">
                    <div class="cover">
                        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                    </div><!-- .cover -->
                </div><!-- .column -->
                <?php elseif($count == 7) : ?>
                <div class="column six squarre">
                    <div class="cover">
                        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('squarre'); } else { ?><?php } ?></a>
                    </div><!-- .cover -->
                    <div class="column three black">
                        <div class="vertical">
                            <div class="round">
                                <div class="time">
                                    <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                                </div><!-- .time -->
                            </div><!-- .round -->
                                <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                                <div class="category">
                                    <?php the_category(''); ?>
                                </div><!-- .category -->
                        </div><div class="vertical-helper"></div><!-- .vertical -->
                    </div><!-- .column -->
                </div><!-- .column -->
                <?php elseif($count == 8) : ?>
                <div class="column three">
                    <div class="cover">
                        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                    </div><!-- .cover -->
                </div><!-- .column -->
                <div class="column three black">
                    <div class="vertical">
                        <div class="round">
                            <div class="time">
                                <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                            </div><!-- .time -->
                        </div><!-- .round -->
                            <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                            <div class="category">
                                <?php the_category(''); ?>
                            </div><!-- .category -->
                    </div><div class="vertical-helper"></div><!-- .vertical -->
                </div><!-- .column -->
                <?php elseif($count == 9) : ?>
                <div class="column three white">
                    <div class="vertical">
                        <div class="round">
                            <div class="time">
                                <?php the_time('M') ?><br /><span><?php the_time('j') ?></span>
                            </div><!-- .time -->
                        </div><!-- .round -->
                            <h2><a href="<?php the_permalink(''); ?>"><?php the_title(''); ?></a></h2>
                            <div class="category">
                                <?php the_category(''); ?>
                            </div><!-- .category -->
                    </div><div class="vertical-helper"></div><!-- .vertical -->
                </div><!-- .column -->
                <div class="column three">
                    <div class="cover">
                        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('small'); } else { ?><?php } ?></a>
                    </div><!-- .cover -->
                </div><!-- .column -->
                <?php endif; ?>
                <?php endwhile; wp_reset_query(); ?>
                <?php else: endif; ?>
                <!-- End Loop -->
                <div class="column twelve">
                    <?php next_posts_link('<div>Older posts &#10093;</div>'); ?>
                    <?php previous_posts_link('<div>&#10092; Newer posts</div>'); ?>
                </div><!-- .column -->
            </div><!-- #content -->
            <!-- End Index
            ================================================== -->
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:1)

建议:

  • 移除位于代码中wp_reset_query(); / next_posts_link正上方的previous_posts_link。在调用导航链接之前,您不想重置主查询。

  • 您实际上不需要自定义计数器,因为您可以使用从0开始的循环索引$GLOBALS['wp_query']->current_post

  • 确保您的查询生成多个页面。

  • 从空循环开始,然后从那里展开。

希望这有帮助。