如何在WordPress查询中重复2个HTML模板?

时间:2017-08-21 作者:Krieg

我正在尝试使用2个HTML模板(模式)进行WP查询。

我需要

我设法制作了第一个模板(A和B),但第二个模板(c和D)遇到了问题,因为它卡在了那个模板中,并用同一个模板循环其余的帖子。

我的代码如下:

<?php 
          $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
          $custom_args = array(
              \'post_type\' => \'causas_p\',
              \'posts_per_page\' => 99,
              \'order\'      => \'ASC\',
              \'orderby\'      => \'menu_order\',
              \'paged\' => $paged
            );
          $custom_query = new WP_Query( $custom_args ); ?>                  
          <?php if ( $custom_query->have_posts() ) : ?>
            <?php $counter = 0; ?>
            <!-- the loop -->
            <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
            <?php $counter ++; ?>
            <div id="c<?php echo $counter;?>"  class="col-lg-3 col-md-6 center col-padding">
            <div class="max-height">
                <div class="common-height clearfix" style="max-width:130px; margin:0 auto">
                    <div class="col-md-4 nopadding"><img class="vertical-middle" src="<?php bloginfo("template_url"); ?>/imagenes/hojita-home-1.png"></div>
                    <div class="col-md-8 nopadding"><h2 class="LunchBoxBold vertical-middle "><?php the_title(); ?></h2></div>
                </div>    
                <p><?php the_field(\'contenido_causasp\');?></p>
                <a class="boton" href="<?php the_permalink(); ?>">Únete</a>
            </div>
              </div>
              <div class="col-lg-3 col-md-6 col-padding" style="background: url(\'<?php the_field(\'imagen_causa\');?>\') center center no-repeat; background-size: cover;">
              </div>
                       <?php if ( $counter >= 2 ) {
                        break;
                        } ;?>

            <?php endwhile; ?>

<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
            <?php $counter ++; ?>
 <div class="col-lg-3 col-md-6 col-padding" style="background: url(\'<?php the_field(\'imagen_causa\');?>\') center center no-repeat; background-size: cover;">
</div>
<div id="c<?php echo $counter;?>" class="col-lg-3 col-md-6 center col-padding">
    <div class="max-height">
        <div class="common-height clearfix" style="max-width:130px; margin:0 auto">
            <div class="col-md-4 nopadding"><img class="vertical-middle" src="<?php bloginfo("template_url"); ?>/imagenes/hojita-home-1.png"></div>
            <div class="col-md-8 nopadding"><h2 class="LunchBoxBold vertical-middle "><?php the_title(); ?></h2></div>
        </div>    
        <p><?php the_field(\'contenido_causasp\');?></p>
        <a class="boton" href="<?php the_permalink(); ?>">Únete</a>
    </div>
</div>
<?php if ( $counter >= 4 ) {
            $counter = 0;

            break;
            } ;?>

           <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        <?php endif; ?> 
我怎样才能在其余的帖子中保持这两种模式?。。。

对不起,我的英语不是我的母语。

提前感谢!

我运气不好:(

我没有任何运气,但我曾经更新过代码。不管怎样,它实际上是把同一个帖子显示了4次,然后转到另一个帖子,重复自己4次,然后转到另一个帖子,然后继续。

我不知道怎么解决这个问题。我认为“if”语句正在重置循环:/。我不知道,请帮忙。

“causas\\u p”,“posts\\u per\\u page”=>99,“order”=>“ASC”,“orderby”=>“menu\\u order”,“paged”=>$paged)$custom\\u query=新的WP\\u查询($custom\\u args);?>have\\u posts()):?>have\\u posts()):$custom\\u query->the\\u post();?>/imagenes/hojita-home-1。png“>

“>Únete”)居中不重复;背景尺寸:封面;”>

                    <?php ( $counter % 4 == 1 ) ;?>
                        <div id="c2"  class="col-lg-3 col-md-6 center col-padding">
                            <div class="max-height">
                                <div class="common-height clearfix" style="max-width:130px; margin:0 auto">
                                    <div class="col-md-4 nopadding"><img class="vertical-middle" src="<?php bloginfo("template_url"); ?>/imagenes/hojita-home-1.png"></div>
                                    <div class="col-md-8 nopadding"><h2 class="LunchBoxBold vertical-middle "><?php the_title(); ?></h2></div>
                                </div>    
                                <p><?php the_field(\'contenido_causasp\');?></p>
                                <a class="boton" href="<?php the_permalink(); ?>">Únete</a>
                            </div>
                              </div>
                              <div class="col-lg-3 col-md-6 col-padding" style="background: url(\'<?php the_field(\'imagen_causa\');?>\') center center no-repeat; background-size: cover;">
                              </div>

                      <?php if( $counter % 4 == 2 ) ;?>
                <div class="col-lg-3 col-md-6 col-padding" style="background: url(\'<?php the_field(\'imagen_causa\');?>\') center center no-repeat; background-size: cover;">
                </div>
                <div id="c3" class="col-lg-3 col-md-6 center col-padding">
                    <div class="max-height">
                        <div class="common-height clearfix" style="max-width:130px; margin:0 auto">
                            <div class="col-md-4 nopadding"><img class="vertical-middle" src="<?php bloginfo("template_url"); ?>/imagenes/hojita-home-1.png"></div>
                            <div class="col-md-8 nopadding"><h2 class="LunchBoxBold vertical-middle "><?php the_title(); ?></h2></div>
                        </div>    
                        <p><?php the_field(\'contenido_causasp\');?></p>
                        <a class="boton" href="<?php the_permalink(); ?>">Únete</a>
                    </div>
                </div>
                <?php if( $counter % 4 == 3 ) ;?>
                <div class="col-lg-3 col-md-6 col-padding" style="background: url(\'<?php the_field(\'imagen_causa\');?>\') center center no-repeat; background-size: cover;">
                </div>
                <div id="c3" class="col-lg-3 col-md-6 center col-padding">
                    <div class="max-height">
                        <div class="common-height clearfix" style="max-width:130px; margin:0 auto">
                            <div class="col-md-4 nopadding"><img class="vertical-middle" src="<?php bloginfo("template_url"); ?>/imagenes/hojita-home-1.png"></div>
                            <div class="col-md-8 nopadding"><h2 class="LunchBoxBold vertical-middle "><?php the_title(); ?></h2></div>
                        </div>    
                        <p><?php the_field(\'contenido_causasp\');?></p>
                        <a class="boton" href="<?php the_permalink(); ?>">Únete</a>
                    </div>
                </div>
                <?php wp_reset_postdata(); ?>
               <?php if ( $counter >= 4 ) {
                            $counter = 0;
                            } ;?>



                           <?php endwhile; ?>
                            <?php wp_reset_postdata(); ?>
                        <?php endif; ?> 

1 个回复
SO网友:deflime

我知道你是ESL,但我想我们需要更多的细节。您需要更好地注释您的代码,这非常令人困惑。

不过我会猜一猜。

看一看Modulo % 操作人员

# Helper to simulate the while, replace this with `$wp_query->get_posts()` or similar
$posts = function() {
    static $posts = 20;
    $posts--;
    return $posts < 0 ?: $posts;
};
$counter = 0;
echo "Looping Posts".PHP_EOL;
while( $posts() ) {
  $counter++;
  echo "Post: {$counter}".PHP_EOL;
  if( $counter % 4 == 1 ) {
    /* template A? */
    echo \' (A)~~> Posts: 1, 5, 9, 13, 17, etc…\'.PHP_EOL;

  } elseif( $counter % 4 == 2 ) {
    /* template B? */
    echo \' (B)~~> Posts: 2, 6, 10, 14, 18, etc…\'.PHP_EOL;

  } elseif( $counter % 4 == 3 ) {
    /* template C? */
    echo \' (C)~~> Posts: 3, 7, 11, 15, 19, etc…\'.PHP_EOL;

  } elseif( $counter % 4 == 0 ) {
    /* template D? */
    echo \' (D)~~> Posts: 4, 8, 12, 16, 20, etc…\'.PHP_EOL;
  }
}
此处示例3v4l.org, 您可以交互尝试。

输出:

Looping Posts
Post: 1
 (A)~~> Posts: 1, 5, 9, 13, 17, etc…
Post: 2
 (B)~~> Posts: 2, 6, 10, 14, 18, etc…
Post: 3
 (C)~~> Posts: 3, 7, 11, 15, 19, etc…
Post: 4
 (D)~~> Posts: 4, 8, 12, 16, 20, etc…
Post: 5
 (A)~~> Posts: 1,

结束

相关推荐

添加Google Analytic in Loop中浏览量最高的帖子

I\'m trying to find a solution to display the 5 post most viewed ( from google analytics) in my loop.The only way I find at the moment is to use this plugin \'Google Analytics Top Content Widget\'However, I don\'t wan\'t to use that and I\'ll wish to be a