【问题标题】:Wordpress Posts odd/even Li layoutWordpress 发布奇/偶李布局
【发布时间】:2014-10-22 03:20:18
【问题描述】:

我正在开发一个自定义主题,并且在其中一个页面上我需要将帖子页面设置为如下样式:http://gyazo.com/e1f7cfc03c7ba3981188077afcdf0314

灰色框是图片,红色框是内容。我可能需要使用奇/偶 Li/Ul 伪类/选择器,但我不知道该怎么做。

谁能给我一个启动它的方法?我正在考虑使用 UL 上的 Odd/Even 伪类来更改 div 名称,但是我不知道该怎么做或从哪里开始。

谢谢!

编辑:我在想奇数/偶数选择器与 jquery prepend/append 结合使用?

Edit2:这是我所拥有的,但它首先显示所有赔率,然后显示所有偶数,而不是交替显示。

PHP:

       <?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>


<div class="section group">

<div class="col span_1_of_2 blue doubleheight">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>

</div>

     <div class="col span_1_of_3_30 blue doubleheight">
     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
     <div class="post-text"><?php the_excerpt(); ?></div>
     </div>


</div>
<?php echo $i; ?>
 <?php endif; endwhile; ?>



<?php while(have_posts()) : ?>

<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>

<div class="section group">
<div class="col span_1_of_3_30 blue doubleheight">
     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="post-text"><?php the_excerpt(); ?></div></div>



<div class="col span_1_of_2 blue doubleheight">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>
</div>

</div>
<?php echo $i; ?>
<?php endif; endwhile; ?>

【问题讨论】:

  • 是的,我已经在原始帖子中发布了我的代码,谢谢
  • 如果你有一个你需要的布局(例如左边的图像,右边的文本),并且只是重复,你可以使用:nth-child(2n)pseudoselector,并使用css使图像浮动(或绝对定位它们)在另一侧。这样一来,其他所有元素都将具有该定位。
  • 问题是,我使用 Div 作为图像,使用 Div 作为信息,所以我不确定这是否可行?
  • 如果它们每次都在具有相同类的包装器中,那么只需定位该包装器。像这样jsfiddle.net/gq5veb9L
  • 这很好用,非常干净,谢谢。

标签: php jquery css wordpress html-lists


【解决方案1】:

由于您处于循环中,您可以使用内置循环计数器 ($wp_query-&gt;current-post) 为所有赔率或所有偶数或两者添加不同的类别

无需运行两个循环。这是我如何在我的网站中使用它来创建两个帖子列的示例

<?php if ( have_posts() ) : ?>

<?php while ( have_posts() ) : the_post();  ?>
        <?php /* Start the Loop */ ?>

        <div class="entry-content-<?php if( $wp_query->current_post%2 == 1 ){ echo ' left-post';}else{ echo ' right-post';} ?>">
        <?php get_template_part( 'content', get_post_format() ); ?>
        </div>

<?php endwhile; ?>

编辑

我在最初的答案中误解了你,但你仍然可以使用我在那里使用的相同想法。这是您可以尝试的方法。用这个替换你所有的代码

<?php if ( have_posts() ) : ?>

    <?php while ( have_posts() ) : the_post();  ?>
    <?php /* Start the Loop */ 

        if($wp_query->current_post%2 == 1 ) { ?>

            <div class="section group">

                <div class="col span_1_of_2 blue doubleheight">

                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>

                </div>

                <div class="col span_1_of_3_30 blue doubleheight">
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <div class="post-text">
                        <?php the_excerpt(); ?>
                    </div>
                </div>


            </div>

        <?php }else{ ?>

            <div class="section group">
                <div class="col span_1_of_3_30 blue doubleheight">
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <div class="post-text">
                        <?php the_excerpt(); ?>
                    </div>
                </div>



                <div class="col span_1_of_2 blue doubleheight">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>
                </div>

            </div>

        <?php }

    <?php endwhile; ?>

 <?php endif; ?>

【讨论】:

  • 这是一种更简洁的处理循环的方法,但是我不确定是否可以在我当前的代码中实现它。我刚刚尝试了一个小时,但无法让它工作。基本上,来自帖子的图像集在一个 div 中,内容(文本/标题)也在一个 div 中,这 2 个 div 在一个容器行中,我调整每个 div 的宽度以从屏幕截图中给出布局的外观.如果您认为我仍然可以实现此功能,请随时告诉我,感谢您的宝贵时间!
  • 我的荣幸。很高兴我能帮忙:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 2014-08-25
  • 2012-05-03
  • 1970-01-01
  • 2012-03-30
相关资源
最近更新 更多