Alternative layout the loop

时间:2016-02-07 作者:Yoeri Rousseaux

我正在尝试创建一个备用循环here 在stackexchange上,这对我迄今为止所取得的成就有很大帮助,但我现在陷入了困境。

目标是使新闻页看起来像这样

newslayout

您可以在以下地址找到开发人员网站:http://puurluc.xact.be/nieuws/

大街区,三个小街区,然后又是一个大街区。然而,当我在一个页面上只有不到5条消息时,整个布局就会变得一团糟(见上面链接的第2页),我对此有点困惑,所以我希望在这里找到一些帮助:)

我想,如果没有足够的帖子,我可以使用if($wp\\u query-current\\u post…)将输出更改为col-md-6或col-md-12但我需要找出处理这个问题的最佳方法,这样我才不会弄乱第一页。

无论如何,索引的代码。php

<?php get_header(); ?>
<section>
<div class="container">
    <div class="row">
        <div class="col-md-8" id="fh5co-content">
             <?php 

            if( have_posts() ) {

                while( have_posts() ) {
                    the_post();

                    get_template_part( \'content\', \'nieuws\' );

                }

                }else{ ?>

                    <p>
                        <?php _e( \'Sorry, no posts matched your criteria.\' ); ?>
                    </p>

                <?php }
            ?>
        </div>
        <div class="col-md-3  col-md-offset-1" id="fh5co-sidebar">

            <div class="fh5co-side-section fh5co-nav-links">
                <h2 class="fh5co-uppercase-heading-sm">Side Links</h2>
                <ul>
                    <li class="active"><a href="#">Design is the Creation</a></li>
                    <li><a href="#">Construction of an Objecton</a></li>
                    <li><a href="#">Architectural Blueprints</a></li>
                    <li><a href="#">Circuit Diagrams</a></li>
                </ul>
            </div>
            <div class="fh5co-spacer fh5co-spacer-sm"></div>
            <div class="fh5co-side-section">
                <h2 class="fh5co-uppercase-heading-sm">Sweet Little &lt;p&gt;</h2>
                <p>Magnam consectetur quia illo rerum aliquam doloremque omnis assumenda quasi enim ex totam provident dignissimos numquam laudantium</p>
                <p><a href="#">Go! click me</a></p>
            </div>
            <div class="fh5co-spacer fh5co-spacer-sm"></div>
            <div class="fh5co-side-section">
                <h2 class="fh5co-uppercase-heading-sm">Image Magnific Popup</h2>


                <div class="fh5co-spacer fh5co-spacer-xxs"></div>
                <div class="row">
                    <div class="col-md-12">
                    <p><a href="images/project_5.jpg" class="image-popup"><img src="images/project_5.jpg" alt="Image" class="img-responsive"></a></p>
                    </div>

                    <div class="col-md-6">
                        <p><a href="images/project_6.jpg" class="image-popup"><img src="images/project_6.jpg" alt="Image" class="img-responsive"></a></p>
                    </div>
                    <div class="col-md-6">
                        <a href="images/project_7.jpg" class="image-popup"><img src="images/project_7.jpg" alt="Image" class="img-responsive"></a></p>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <div class="fh5co-spacer fh5co-spacer-md"></div>
    <?php 
                if (function_exists("wp_bs_pagination"))
                    {
                         //wp_bs_pagination($the_query->max_num_pages);
                         wp_bs_pagination();
                }
            ?> 
</div>  
</section>
<?php get_footer(); ?>
我的新闻模板内容的代码nieuws。php

<?php
//echo $wp_query->found_posts;
//var_dump($wp_query);die();
    >if( $wp_query-current_post == 0 ) : ?>
<h2><?php the_title(); ?></h2>
<div class="row">
    <div class="col-md-6">
        <p><?php the_excerpt(); ?></p>
    </div>
    <div class="col-md-6">
        <p><?php the_excerpt(); ?></p>
    </div>
    <div class="col-md-12">
    <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
    </div>
</div>
<div class="fh5co-spacer fh5co-spacer-sm"></div>
<?php else : ?>
    <?php if($wp_query->current_post == 1) : ?>
        <div class="row">
    <?php endif; ?>
        <?php if($wp_query->current_post == 4) : ?>
            <h2><?php the_title(); ?></h2>
                <div class="row">
                    <div class="col-md-6">
                        <p><?php the_excerpt(); ?></p>
                    </div>
                    <div class="col-md-6">
                        <p><?php the_excerpt(); ?></p>
                    </div>
                    <div class="col-md-12">
            <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
            </div>
                </div>
        <?php else : ?>
        <div class="col-md-4 col-sm-6">
            <div class="fh5co-spacer-xs"></div>
            <h3><?php the_title(); ?></h3>
            <p><?php the_excerpt(); ?></p>
            <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
        </div>
        <?php endif; ?>
    <?php if($wp_query->current_post == 3) : ?>
        </div>
    <?php endif; ?>
<?php endif; ?>
真的希望有人能帮我解决这个问题!

1 个回复
最合适的回答,由SO网友:Yoeri Rousseaux 整理而成

对于那些感兴趣的人,我已经使用sizeof($wp query->posts)解决了我的问题

最终的结果就是这条内容新闻。php模板

<?php
//echo sizeof($wp_query->posts);
//var_dump($wp_query);die();

if(sizeof($wp_query->posts) == 4) : ?>
    <?php if( $wp_query->current_post == 0 ) : ?>
        <h3><?php the_title(); ?></h3>
        <div class="row">
            <div class="col-md-12">
                <p><?php the_excerpt(); ?></p>
                <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
            </div>
        </div>
        <div class="fh5co-spacer fh5co-spacer-sm"></div>
    <?php else : ?>
        <?php if($wp_query->current_post == 1) : ?>
            <div class="row row-eq-height">
        <?php endif; ?>
                <div class="col-md-4 col-sm-6">
                    <h3><?php the_title(); ?></h3>
                    <p><?php the_excerpt(); ?></p>
                    <div class="fh5co-spacer-xs"></div>
                    <p class="bottom-align-text"><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
                </div>
        <?php if($wp_query->current_post == 3) : ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>
<?php elseif(sizeof($wp_query->posts) == 3) : ?>
    <?php if( $wp_query->current_post == 0 ) : ?>
        <h3><?php the_title(); ?></h3>
        <div class="row">
            <div class="col-md-12">
                <p><?php the_excerpt(); ?></p>
            <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>

            </div>
        </div>
        <div class="fh5co-spacer fh5co-spacer-sm"></div>
    <?php else : ?>
        <?php if($wp_query->current_post == 1) : ?>
            <div class="row row-eq-height">
        <?php endif; ?>
            <div class="col-md-6 col-sm-6">
                <h3><?php the_title(); ?></h3>
                <p><?php the_excerpt(); ?></p>
                <div class="fh5co-spacer-xs"></div>
                <p class="bottom-align-text"><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
            </div>
        <?php if($wp_query->current_post == 2) : ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>
<?php elseif(sizeof($wp_query->posts) == 1 || sizeof($wp_query->posts) == 2) : ?>
<h3><?php the_title(); ?></h3>
<div class="row">
    <div class="col-md-12">
        <p><?php the_excerpt(); ?></p>
        <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
    </div>
</div>
<div class="fh5co-spacer fh5co-spacer-sm"></div>
<?php else : ?>
<?php if( $wp_query->current_post == 0 ) : ?>
<h3><?php the_title(); ?></h3>
<div class="row">
    <div class="col-md-12">
        <p><?php the_excerpt(); ?></p>
        <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
    </div>
</div>
<div class="fh5co-spacer fh5co-spacer-sm"></div>
<?php else : ?>
    <?php if($wp_query->current_post == 1) : ?>
        <div class="row row-eq-height">
    <?php endif; ?>
        <?php if($wp_query->current_post == 4) : ?>
            <h3><?php the_title(); ?></h3>
                <div class="row">
                    <div class="col-md-12">
                        <p><?php the_excerpt(); ?></p>
                        <p><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
                    </div>
                </div>
        <?php else : ?>
        <div class="col-md-4 col-sm-6">
            <h3><?php the_title(); ?></h3>
            <p><?php the_excerpt(); ?></p>
            <div class="fh5co-spacer-xs"></div>
            <p class="bottom-align-text"><a href="<?php the_permalink(); ?>" class="btn btn-primary btn-sm">Lees Meer</a></p>
        </div>
        <?php endif; ?>
    <?php if($wp_query->current_post == 3) : ?>
        </div>
        <div class="fh5co-spacer fh5co-spacer-sm"></div>
    <?php endif; ?>
<?php endif; ?>
<?php endif; ?>

相关推荐

将wp_Query替换为wp_User_Query

我在插件中制作了一些订阅者档案和单曲(个人资料页),其中我还包括了一个“用户单曲”模板,通过template_include. 不过,我正在尝试从插件中删除一些模板,以使其使用主题模板。我用了locate_template( \'single.php\' ) 从活动主题中选择单个模板。我没有使用全局wp_query 在本例中显示我的内容,但页面显示了基于查询默认值的循环(十篇帖子)。我想知道的是,我是否可以完全放弃默认查询,用wp_user_query 我可以将查询到的用户ID输入其中。然后我想筛选the