【问题标题】:Display 3 columns to be read alphabetically down each column显示 3 列,每列按字母顺序阅读
【发布时间】:2016-06-30 14:18:27
【问题描述】:

我买了一个wordpress模板,尝试修改品牌页面的代码显示3栏。以下是场景:

当前显示:

a | b | c | d
e | f | g | h
i | j | k

我想让显示器变成

a | d | g 
b | e | h 
c | f | i

下面的代码显示当前代码。谁能帮我更正下面的代码以显示上述情况。

代码:

<?php
   $args = array(
    'post_type' => 'partner',
    'posts_per_page' => '-1',        
    'order'          => 'ASC',
    'post__not_in' => array( $post->ID )
   );

  // the query
  $query = new WP_Query( $args );

  // The Loop
     ?>
  <section class="recent-posts clear">
    <?php           
        $lastChar = ''; 
        $count_posts = wp_count_posts('partner')->publish;
        $i = 0;
    ?>
    <?php if ($query->have_posts()) : while($query->have_posts()) : $i++;
   if(($i % 3) == 0) : else : $query->the_post(); ?>
    <?php 
        global  $post;
        $brandname = $post->post_title;
        $char = $brandname[0];
    ?>
    <div style="float:left; width:24%; margin-right:10px;">
        <?php 
                if ($char !== $lastChar) {
                        if ($lastChar !== '')
                            echo '<br>';                        
                            echo "<div style='padding:10px; background:red;
 color:white; font-weight:bold;'>" .strtoupper($char)."</div>"; 
//print A / B / C etc
                            $lastChar = $char;
                }   
                echo $brandname; 
            ?>
        <?php the_content(); ?>
    </div>

    <?php endif; endwhile; else: ?>
    <div>Alternate content</div>
    <?php endif; ?>


  </section>

【问题讨论】:

    标签: wordpress post types multiple-columns


    【解决方案1】:

    在你的代码中尝试删除

    <div style="float:left; width:24%; margin-right:10px;">
    

    替换成

    <div style="float:left; width:32%; margin-right:11px;">
    

    【讨论】:

    • 那行不通。检查当前显示的屏幕截图。
    • 您的列是 24%,应该适合其中的 4 个而没有问题。但是,您有 10 像素的右边距。尝试将右边距从 10px 减小到更小的值。最好从 0 开始
    • 嗨 Janvier 感谢您的建议,但问题不在于列宽。上面的代码显示 3 列但是它显示 a,b,c,d ... 水平而不是 a,b,c,d ... 垂直
    【解决方案2】:

    给你。您只需要在循环中进行一些计数和编辑。见代码中的 cmets -

    <?php 
    $count_partners = wp_count_posts('partner');
    $count_posts = $count_partners->publish;
    $i = 0;
    //get the number of items for each column
    $items_per_column = $count_posts/3;
    if (($count_posts % 3) == 1) { $first_col = $items_per_column + 1;
                            $second_col = $items_per_column;
                            $third_col = $items_per_column;
    } elseif (($i % 3) == 2) { $first_col = $items_per_column + 1;
                            $second_col = $items_per_column  + 1;
                            $third_col = $items_per_column;
    } else {
        $first_col = $items_per_column;
        $second_col = $items_per_column;
        $third_col = $items_per_column;
    
    }
    
    if ($query->have_posts()) : while($query->have_posts()){
         $query->the_post(); 
    
            global  $post;
            $brandname = $post->post_title;
            $char = $brandname[0];
        if ( $i == 1) {//for the first item start a column div
            ?>
            <div style="float:left; width:24%; margin-right:10px;">
            <?php 
        }
                    if ($char !== $lastChar) {
                            if ($lastChar !== '')
                                echo '<br>';                        
                                echo "<div style='padding:10px; background:red;
     color:white; font-weight:bold;'>" .strtoupper($char)."</div>"; 
    //print A / B / C etc
                                $lastChar = $char;
                    }   
                    echo $brandname; 
                ?>
            <?php the_content();
            if ( $i == $first_col || $i == $second_col ) {
            //for first and second cols close the div and start the next
                ?>
                </div>
                <div style="float:left; width:24%; margin-right:10px;">
    
                <?php 
            }
    
            if ( $i == $third_col ) {
            //for the third col just close it up
                ?>
                </div>
    
                <?php 
            }
    
            $i++;
        } else: ?>
        <div>Alternate content</div>
    

    已更新 - 稍微更改了语法并将计数器移到了 while 循环的末尾。

    【讨论】:

    • 嗨@dg4220,感谢您的帮助。我试过了,但它全部显示在 1 列中。
    • 查看我的编辑有错字。这: if (($i % 3) == 1) { $first_col = $items_per_column + 1;应该是 if (($count_posts % 3) == 1) { $first_col = $items_per_column + 1;如果还有其他问题,我稍后会进行更彻底的测试。
    • 另外,只需检查计数器并确保数字正确;确保 $i 正在递增,等等。稍后我将能够解决更多问题,但希望让您了解正确的概念
    • 还是不行。它只显示在 1 列中。谢谢你,等你。
    • 已更新以修复被错误调用的 wp_count_posts()(检查代码)您检查计数器的输出了吗?那将确定问题。如果您在输出计数器的值时遇到问题,请立即尝试,并确保您得到了您期望的结果。如果没有,请添加有关您尝试过的内容、计数器输出是什么等的更多信息。我已经解决了上述问题,但我不确定是否应该为您检查所有代码?
    【解决方案3】:

    试试这个代码

    $args = array(
        'post_type' => 'partner',
        'posts_per_page' => '-1',        
        'order'          => 'ASC',
        'post__not_in' => array( $post->ID )
       );
    $list = new WP_Query( $args );
    $count_posts = wp_count_posts('partner');   
    $lastChar = '';  $inc=1;
    
    if ( $list->have_posts() ) :
    echo "<div class='colm'>";
    while($list->have_posts()): $list->the_post();
        global  $post;  
    
        $brandname = $post->post_title;
        $char = $brandname[0];                      
    
    
        if ($char !== $lastChar) {
                     //if($inc!=0){echo "</div>";}$inc++;
    
            if($inc%4==0){echo "</div><div class='colm'>";$inc=1;}
                 $inc++;             
                     echo "<div style='padding:10px; background:red;
                            color:white; font-weight:bold;'>".
                         strtoupper($char)."</div>";
    
                $lastChar = $char;
        }  
        echo '<li><a href="'.$post->link.'" 
                     title="'.$post->post_title.'"
                     target="_BLANK"    >' .
             $brandname.$inc.'</a></li>';                        
    
    
        endwhile;
        echo "</div>";
        endif;
    
        wp_reset_postdata();    
    

    添加这个 CSS

    .colm{
            float:left;
            margin: 10px;
            width: 30%;
        }
    

    【讨论】:

    • 谢谢@Ankur Bhadania,现在一切正常。我只是将值 $inc%4 更改为 1 并显示我需要的内容。非常感谢。
    • 我也使用 masonry css 技术来得到我的结果,如上图所示。
    【解决方案4】:

    在一组垂直的 3 列中实现结果的加法是砌体 css 技术。这是我的最终结果:

    <div id="masonry-container" class="cols">
    <?php      
    
    $args = array(
    'post_type' => 'partner',
    'posts_per_page' => '-1',   
    'orderby'=> 'title',     
    'order'          => 'ASC',
    'post__not_in' => array( $post->ID ));
    
    $list = new WP_Query( $args );
    $count_posts = wp_count_posts('partner');   
    $lastChar = ''; 
    $inc=1;
    
    if ( $list->have_posts() ) :
    
        echo "<div style='colm'>";
    
        while($list->have_posts()): $list->the_post();
    
        global  $post;  
    
        $brandname = $post->post_title;
        $char = $brandname[0];                      
    
    if ($char !== $lastChar) {                 
    
        if($inc%1 == 0) {echo "</div><div class='colm'>";}
    
            $inc++;            
    
            echo "<div style='padding:10px; background:red; color:white; font-weight:bold;'>".strtoupper($char)."</div>";
            echo "<br>";
            $lastChar = $char;
    }  
        echo '<li><a href="'.$post->link.'" title="'.$post->post_title.'" target="_BLANK">' .$brandname.'</a></li>'; 
    
        endwhile;
        echo "</div>";
        endif;
    
    wp_reset_postdata();   
    
    ?>
    </div>
    

    CSS

    #masonry-container {
        width: 100%;
        max-width: 1200px;
        margin: 2em auto;
    }
    
    .cols {
       -moz-column-count:3;
       -moz-column-gap: 3%;
       -moz-column-width: 30%;
       -webkit-column-count:3;
       -webkit-column-gap: 3%;
       -webkit-column-width: 30%;
       column-count: 3;
       column-gap: 3%;
       column-width: 30%;
    }
    
    #contents .colm {
       margin:10px;
    }
    #contents .colm li{
       list-style: circle;
       margin-left:30px;
       line-height:25px;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2017-01-25
      • 2018-07-13
      • 1970-01-01
      相关资源
      最近更新 更多