在不同的列中显示wordpress帖子

时间:2016-06-13 09:08:00

标签: php wordpress

我试图在不同的列中显示我的博客页面中的wordpress帖子。 示例:两行中的前两个帖子,两列中的剩余帖子一个接一个地相同。

如何展示与附件相同的帖子?

先谢谢!!

enter image description here

这是我的代码。

<div class="container">
<div class="row">
    <div class="col-xs-12 col-sm-6">
                <?php
                $news_args = array(
                    'cat' => 3,
                    'paged' => get_query_var('paged'),
                    'posts_per_page' => 4,
                    'orderby' => 'post_date',
                    'order' => 'DESC',
                );
                $news_query = new WP_Query($news_args);
                if ($news_query->have_posts()) :
                while ($news_query->have_posts()) : $news_query->the_post();
                ?>

                                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                    <p><span><?php the_time('F j, Y'); ?>
                                    </span>
                                    <span><?php echo "By: ".get_the_author(); ?>
                                    </span>
                                    <span>
                                    <?php
                                    $commentcount = comments_number('0', '1', '%');
                                    $fulltitle = $title . ' (' . $commentcount . ' comments)';
                                    ?>Comments<?php wp_count_comments( $post_id ); ?> 
                                    </span></p>

                                <figure class="fig_rel1">
                                <?php the_post_thumbnail(); ?>
                                </figure>
                                <p>
                                <?php
                                $content = get_the_content('',FALSE,'');
                                $content = apply_filters('the_content', $content);
                                $content = str_replace(']]>', ']]>', $content);
                                echo substr($content,0,300).'<a href="'. get_permalink($post->ID) .'">Read More &raquo; </a>';
                                ?>
                                </p>

                <?php
                endwhile;
                previous_posts_link('prev', $news_query->max_num_pages);
                echo ' &mdash; ';
                next_posts_link('next', $news_query->max_num_pages);
                endif;
                ?>

                </div>
                </div>
                </div>

3 个答案:

答案 0 :(得分:1)

它可以帮助你。

$news_args = array(
    'cat' => 3,
    'paged' => get_query_var('paged'),
    'posts_per_page' => 4,
    'orderby' => 'post_date',
    'order' => 'DESC',
);

$news_query = new WP_Query($news_args);

$count = 0;

if ($news_query->have_posts()) :
while ($news_query->have_posts()) : $news_query->the_post();

    if ( $count < 2 ) {
       //code to populate first two post
       $count++;
    }else
       //code to populate remaining posts
       $count++;
    }

endwhile;

答案 1 :(得分:0)

尝试实施以下方法。

$count = 0;

while (have_posts()) : the_post();

    if ( $count < 2 ) {
       //code to populate first two post
       $count++;
    }else
       //code to populate remaining posts
       $count++;
    }

endwhile;

答案 2 :(得分:0)

最后我得到了解决方案,如果条件分为&#34; div&#34;标签

这是最终解决方案。

<div class="container">
<div class="row">
    <div class="col-xs-12 col-sm-8">
        <div class="container-fluid">
            <div class="row">
                <div class="col-xs-12 col-sm-12">
                <div class="row">
                <div class="ka-justcontent">
                 <?php
                $news_args = array(
                    'cat' => 3,
                    'paged' => get_query_var('paged'),
                    'posts_per_page' => 4,
                    'orderby' => 'post_date',
                    'order' => 'DESC',
                );
                $news_query = new WP_Query($news_args);
                $i = 0;
                if ($news_query->have_posts()) :
                while ($news_query->have_posts()) : $news_query->the_post();
                ?>
                            <div <?php if($i<2){?> class="col-md-12" <?php } else {?> class="col-xs-12 col-sm-6 col-md-6" <?php }?> >
                            <!-- Previous Next -->
                                <div class="survey">
                                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                    <p><span><?php the_time('F j, Y'); ?>
                                    </span>
                                    <span><?php echo "By: ".get_the_author(); ?>
                                    </span>
                                    <span>
                                    <?php
                                    $commentcount = comments_number('0', '1', '%');
                                    $fulltitle = $title . ' (' . $commentcount . ' comments)';
                                    ?>Comments<?php wp_count_comments( $post_id ); ?> 
                                    </span></p>
                                </div>
                                <figure class="fig_rel1">
                                <?php the_post_thumbnail(); ?>
                                </figure>
                                <p>
                                <?php
                                $content = get_the_content('',FALSE,'');
                                $content = apply_filters('the_content', $content);
                                $content = str_replace(']]>', ']]>', $content);
                                echo substr($content,0,300).'<a href="'. get_permalink($post->ID) .'">Read More &raquo; </a>';
                                ?>
                                </p>
                            </div>
                <?php
                $i++;
                endwhile;
                previous_posts_link('prev', $news_query->max_num_pages);
                echo ' &mdash; ';
                next_posts_link('next', $news_query->max_num_pages);
                endif;
                ?>
                </div>
                </div>
                </div>              
            </div>
        </div>
    </div>
</div>