【问题标题】:3 posts in one row, and then two posts in the next row etc一行 3 个帖子,然后下一行有 2 个帖子,依此类推
【发布时间】:2018-06-21 11:12:25
【问题描述】:

我需要一些帮助来修改 Wordpress 循环。我想要一个布局,每第二行有 3 个帖子而不是两个。所以我想实现以下目标:

输出:
中6大6,中6大6,
中 4 大 4、中 4 大 4、中 4 大 4、
中 6 大 6, 中 6 大 6,
中 4 大 4、中 4 大 4、中 4 大 4
...等

我尝试了以下方法,该方法适用于第一行,但之后不同步。

非常感谢。

如果(有_posts()): $post_i = 0; /* 开始循环 */ 而(有_posts()):the_post(); $post_i++; if( ($post_i + 1 ) % 4 == 0 && $post_i > 0 ) $post_class= '中 6 大 6'; 否则 if( ($post_i + 1 ) % 5 == 0 && $post_i > 0 ) $post_class= '中 6 大 6'; 否则 $post_class= 'medium-4 large-4';

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    你需要使用偶数条件来显示像2 posts, 3 posts, 2 posts, 3 posts ... etc这样的帖子

    if (have_posts()) :
    
        $post_i = 0;
    
        /* Start the Loop */
        while (have_posts()) : the_post();
    
            if ($post_i % 2 == 0)
                $post_class = 'medium-6 large-6'; 
            else
                $post_class = 'medium-4 large-4';
    
            var_dump($post_class);
    
            $post_i++;
        endwhile;
    endif;
    

    输出:

    1. 中6大6
    2. 中4大4
    3. 中6大6
    4. 中4大4
    5. .....等

    【讨论】:

    • 感谢您回答我的问题,但我认为我没有很好地解释自己。我正在寻找一个看起来像这样的输出。输出:中6大6、中6大6、中4大4、中4大4、中4大4、中6大6、中6大6 ,中4大4,中4大4,中4大4,......等
    • 你没有在问题中说
    • 我认为这是我的问题令人困惑的方面。 “示例:2 个帖子 3 个帖子 2 个帖子 3 个帖子”。
    • 那么运气不好?
    【解决方案2】:

    我想通了。可能不是最好的解决方案,但它对我有用。我做了以下操作:

    如果 (have_posts()) : $post_i = 0; /* 开始循环 */ while (have_posts()) : the_post(); 如果 ($post_i % 5 == 0) $post_class= '中 6 大 6'; 否则如果 ($post_i % 5 == 1) $post_class= '中 6 大 6'; 否则 $post_class= 'medium-4 large-4'; $post_i++; var_dump($post_class); 结束; 万一;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2020-09-20
      • 1970-01-01
      • 2015-07-24
      相关资源
      最近更新 更多