duanming7833 2016-11-26 17:13
浏览 18

WordPress中不同div块中的每隔一个帖子

Let's say I have 2 styles. Style 1 and Style 2 I would like to show every odd number post in the style 1 and even number posts in style 2

Below is my template file:

<?php /* Template Name: Home v2 */ get_header(); ?>
<main role="main">
    <!-- section -->
    <section>
        <?php 
        $temp = $wp_query; $wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query('showposts=' . $theme_options['blog-post-per-page'] . '&paged='.$paged);
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 

        <div class="tiles-block w-clearfix">
            <div class="float-left">
                <div class="content-block w-clearfix">
                    <h5 class="black travel"><?php echo the_category();?></h5>
                    <h1 class="black mastheading"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
                    <div class="date-block w-container">
                        <div class="black mini-date"> <?php echo get_the_date(); ?></div>
                        <img class="mini-icon" src="<?php echo get_template_directory_uri() . '/images/comment-icon.png' ?>">
                        <div class="black mini-date">32</div>
                        <img class="mini-icon" src="<?php echo get_template_directory_uri() . '/images/heart-icon.png' ?>">
                        <div class="black mini-date">14</div>
                    </div>
                    <div class="black iltalica"><?php the_excerpt(); ?></div>
                    <a class="a8 cta w-button" href="<?php the_permalink(); ?>">continue reading</a>
                </div>
            </div>
            <div class="float-left natural-forest">
                <div class="thumb">
                    <?php the_post_thumbnail('full'); ?>
                </div>
            </div>
            <img class="right-arrow" src="<?php echo get_template_directory_uri() . '/images/right-arrow.png' ?>" />
        </div>

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

The code above works good. but it gives only style 1. I already have html setup for style 2 but don't know how to implement in the code above.

This is my style2 block

<div class="tiles-block w-clearfix">
                <div class="float-right">
                    <div class="content-block w-clearfix">
                        <h5 class="black photography">PHOTOGRAPHY</h5>
                        <h1 class="black mastheading">title test</h1>
                        <div class="date-block w-container">
                            <div class="black mini-date">October 26, 2016</div>
                            <img class="mini-icon" src="http://aa.com/aa.png">
                            <div class="black mini-date">15</div>
                            <img class="mini-icon" src="http://bb.com/bb.png">
                            <div class="black mini-date">13</div>
                        </div>
                        <div class="black iltalica">Lorem Ipsuim dolor....</div>
                        <a class="a8 cta w-button" href="#">continue reading</a></div>
                    </div>
                <div class="float-left window-light">
                    <div class="thumb">
                        <img src="<?php echo get_template_directory_uri() . '/images/post2.png' ?>" />
                    </div>
                </div>
                <img class="left-arrow" src="http://cc.com/cc.png">
            </div>
  • 写回答

2条回答 默认 最新

  • dqvs45976 2016-11-26 21:41
    关注

    This will get a little messy in PHP because you'd need several if statements. You might consider naming some of the inner divs the same, and making only the outer divs different names, and doing more of the work in CSS. That said, here's how you would do it:

    Set a flag that changes each time through the loop, and check the value of the flag before you output your HTML.

    $style_flag = 1;
    while ($wp_query->have_posts()) : $wp_query->the_post();
       //check the value
       if ($style_flag == 1){
           // output style 1
       } else {
          // output style 2
       }
       // do everything else you need to do in the loop
    
       // switch the value of the flag
       $style_flag = $style_flag * -1;
    end while;
    

    Back to CSS for a moment, though. Check out the nth-child selector if you're not familiar with it already: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制