2014-01-18 21 views
5

私はBootstrapで作成したテーマをWordPressに統合しています。私は現在、縦書きではなく横書きで表示するという課題に直面しています。デザインは3列を使用します。WordPressの投稿を水平に3列に表示するにはどうすればよいですか?

このサイト (http://perishablepress.com/two-column-horizontal-sequence-wordpress-post-order/)に掲示2列のためのソリューション は参考になりましたが、3列で使用した場合には、以前に表示されたポストの繰り返しをポストします。

<div class="row"> 

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

    <div class="col-sm-4"> 
    <img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" > 
    <h3><?php the_field('description'); ?></h3> 

    </div> 

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

    <?php $i = 0; rewind_posts(); ?> 

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

    <div class="col-sm-4"> 
    <img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" > 
    <h3><?php the_field('description'); ?></h3> 
    </div> 

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


    <?php $i = 0; rewind_posts(); ?> 

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

    <div class="col-sm-4"> 
    <img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" > 
    <h3><?php the_field('description'); ?></h3> 
    </div> 

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



    </div> 

すべてのヘルプは感謝される:

は、ここに私のコードです。

ありがとうございます。

答えて

1

この例を見てみましょう。あなたが望むように動作し、この例に従ってコードを整理してください。

$i = 1; 
echo "<div class='row'>\n"; 
while($i <= 10){ 

    echo " <div class='col-lg-4'></div>\n"; 
    if($i % 3 == 0) { echo "</div>\n<div class='row'>\n"; } 

    $i++; 
} 
echo "</div>\n"; 

http://codepad.org/Qesw28Cw

+0

これをコードにどのように統合しますか?私はいくつかの方法を試しましたが、それは私のために働いていません。 –

1

それからhave_posts()ループの後の行をエコー、ダイナミックアレイにHTML文字列を構築しました。これにより、投稿数が4で除算され、4列で垂直方向に並べ替えられます。ここに私の例があります:

$query = new WP_Query(array(
         'post_status' => 'publish', 
         'orderby'  => 'title', 
         'order'   => 'ASC', 
         'posts_per_page' => -1 
         )); 

$post_count = $query->post_count; 
$posts_per_column = ceil($post_count/4);  

$rows = array();            
$count = 0; 
while ($query->have_posts()) 
{ $query->the_post(); 
    if($rows[$count] == ""){ $rows[$count] = '<div class="row">'; } 
    $rows[$count] = $rows[$count] . '<div class="col-sm-3">' . 
      '<div class="post-title"> 
      <a href="'.get_permalink().'">'.get_the_title().'</a></div>' . 
      '<div class="post-author">by '. get_the_author() .'</div></div>'; 
    $count++;       
    if ($count == $posts_per_column) { $count = 0; } 
} 

foreach ($rows as $row) { echo $row . '</div>'; } 
+0

これを3列にするには、$ posts_per_column = ceil($ post_count/4)を変更します。 $ posts_per_column = ceil($ post_count/3)までです。それに応じてcolクラスを作成します。 –

関連する問題