【问题标题】:wordpress loop – different html for every 2nd custom postwordpress 循环 - 每个第二个自定义帖子都有不同的 html
【发布时间】:2016-11-08 16:30:24
【问题描述】:

我一直在寻找一种解决方案来设置我的自定义帖子循环,如下所示:

first post> img left, content right // 第二个帖子> content left, img right

这是我目前的代码:

<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) :  ?>

   <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>
                </div>
              </article>
<?php else : ?>

   <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-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>
                    <div class="col col-4">
                           <?php the_post_thumbnail(600); ?>
                </div>

                </div>
              </article>
<?php endif; endwhile; endif; ?>
</div>

我知道,这个问题已经被问过好几次了,我已经尝试过thisthis 并且我读过this 但对我没有任何用处。我做错了什么?

【问题讨论】:

  • 那么你现在的结果是什么?全部左对齐还是全部右对齐?
  • 结果很奇怪。这个网站上已经添加了 3 个帖子,但我只得到了条目标题,但它没有给出帖子的条目标题,它会生成页面的条目标题(如果有任何意义的话)。跨度>
  • 我已经得到了这个代码(没有改变):

标签: php wordpress loops


【解决方案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) :  ?>

请注意在您设置帖子对象和参数的位置添加了 $loop 变量。

【讨论】:

  • 嘿史蒂夫!这完全有道理。现在可以了!非常感谢!
  • 没问题 :) 很高兴能帮上忙。
【解决方案2】:

我已经得到了这段代码(没有改变):

                <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-->

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多