wordpress循环 - 每个第二个自定义帖子的不同html

时间:2016-11-08 16:30:25

标签: php wordpress loops

我正在寻找一个解决方案来设置我的自定义post循环:

第一篇文章> img left,content right // second post>内容离开,img right

到目前为止,这是我的代码:

declare @t table(ID int
                ,Number int
                ,Condition1 int
                ,Condition2 int
                ,Condition3 int
                ,Condition4 int
                );
insert into @t values
 (1,1,1,2,1,1)
,(1,2,2,1,2,2)
,(2,5,2,2,2,1)
,(2,6,2,2,2,2)
,(3,7,1,1,2,1)
,(3,8,2,1,1,2)
,(3,3,2,1,2,2)
,(4,9,2,1,1,1)
,(4,4,1,1,1,2)
,(5,10,2,1,2,1)
,(5,13,2,1,2,2);


with cte as
(
    select row_number() over (order by ID
                                    ,Condition1
                                    ,Condition2
                                    ,Condition3
                                    ,Condition4
                            ) as rn
            ,ID
            ,Number
            ,Condition1
            ,Condition2
            ,Condition3
            ,Condition4
            ,case when Condition1 = 1 then 1
                else case when Condition2 = 1 then 1
                    else case when Condition3 = 1 then 1
                        else case when Condition4 = 1 then 1
                            else 2
                            end
                        end
                    end
                end as IsOk
    from @t
)
select c1.ID
        ,c1.Number
        ,c1.Condition1
        ,c1.Condition2
        ,c1.Condition3
        ,c1.Condition4
        ,case when isnull(c2.IsOk,0) = 1 then 2 else c1.IsOk end as IsOk
from cte c1
    left join cte c2
        on(c1.ID = c2.ID
            and c1.rn = c2.rn+1
            )
order by c1.rn;

我知道,这个问题已被问过几次,我已经尝试thisthis了,我读了this但是对我来说没什么用。我做错了什么?

2 个答案:

答案 0 :(得分:1)

我想它只输出一个帖子,该帖子实际上是附加到页面的帖子内容。问题是如何在页面中初始化附加循环。您正在创建一个新的post对象 - 但是您没有将它分配给if / while语句:

<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) :  ?>

应该是:

<?php if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); $i++; if(($i % 2) == 0) :  ?>

注意在你设置post对象和参数时添加了$ loop变量。

答案 1 :(得分:0)

我已经使用了这段代码(没有改变):

                <div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
 <article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
                <div class="row centered wow fadeInUpBig" data-wow-duration="2s">   
                    <div class="col col-4">
                           <?php the_post_thumbnail(600); ?>
                </div>
                <div class="col col-6">
                <section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span> 
                       <h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
                  <?php the_content();?>
                </section> 
                </div><!--.end col-->
                </div><!--.end row-->
              </article>
<?php endwhile; wp_reset_query(); ?>
                </div><!--.end container-->