【问题标题】:Navigation with post-count in Wordpress loop在 Wordpress 循环中使用后计数进行导航
【发布时间】:2014-04-13 09:35:14
【问题描述】:

导航不适用于在我的 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(); ?>

【问题讨论】:

    标签: wordpress navigation


    【解决方案1】:

    建议:

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

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

    • 确保您的查询生成的页面不止一页。

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

    希望这会有所帮助。

    【讨论】:

    • 非常感谢你,birgire!感谢您的建议,我发现了问题所在。这很愚蠢:在博客管理的“阅读”部分配置的要显示的帖子数量高于我的查询定义的帖子数量。现在,它完美运行。再次感谢!
    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多