简体   繁体   English

如何将WordPress帖子水平显示为3列?

[英]How do I display WordPress posts into 3 columns horizontally?

I am integrating my theme built with Bootstrap into WordPress and I am now faced with the challenge of displaying my posts horizontally instead of vertically. 我正在将使用Bootstrap构建的主题集成到WordPress中,现在我面临着水平而不是垂直显示帖子的挑战。 The design uses 3 columns. 该设计使用3列。

The solution for two columns posted at this site ( http://perishablepress.com/two-column-horizontal-sequence-wordpress-post-order/ ) was helpful but it posts repeats of previously displayed posts when used with 3 columns. 在这个网站上发布的两个专栏的解决方案( http://perishablepress.com/two-column-horizo​​ntal-sequence-wordpress-post-order/ )很有帮助,但是当与3列一起使用时,它会重复发布先前显示的帖子。

Here is my code: 这是我的代码:

    <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>

Any help would be grateful. 任何帮助将不胜感激。

Thank you. 谢谢。

Take a look on this example, Its working like you want, and arrange your code according to this example. 看看这个例子,它的工作方式与你想要的一样,并根据这个例子安排你的代码。

$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 http://codepad.org/Qesw28Cw

I built the html strings into a dynamic array, then echo the rows after the have_posts() loop. 我将html字符串构建为动态数组,然后在has_posts()循环之后回显行。 This divides the number of posts by 4 then orders them vertically in 4 columns. 这将帖子的数量除以4然后在4列中垂直排序。 Here is my example: 这是我的例子:

$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>'; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以四列格式从左到右水平显示动态创建的帖子 - How can I display the dynamically created posts horizontally from left to right in a four columns format 如何在三列中显示wordpress帖子? - How to display wordpress posts in three columns? Wordpress - 如何显示按自定义分类法分组的帖子? - Wordpress - How do I display posts grouped by custom taxonomy? 如何在帖子页面显示WordPress的所有分类? - How do I display all categories in WordPress on posts page? 如何在自定义Wordpress循环中隐藏过去的帖子并显示即将发布的X个帖子? - How do I hide past posts in a custom Wordpress loop AND display X number of upcoming posts? 如何在图像下显示多个相等列的WordPress博客帖子? - How to display WordPress blog posts with multiple equal columns under image? 如何设置交替的WordPress帖子的样式? - How do I style alternating WordPress posts? WordPress,我如何AJAX附加帖子 - WordPress, How Do I AJAX Additional Posts 如何查询帖子自定义关系字段并将解析的搜索词包含在循环中并在 WordPress 中显示结果 - How do I query posts custom relationship field and include parsed search term into loop and display results in WordPress 如何显示四个彼此对齐的类别帖子? -WordPress的 - How do I display four category posts that align next to each other? - Wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM