个性化阅读
专注于IT技术分析

如何打印wordpress帖子?

点击下载

众所周知, 要在wordpress上显示帖子, 我们必须使用循环。但通常, 用于在wordpress上显示帖子的循环如下

$query = new WP_Query( $args );
$query = $query->get_posts();
	
foreach ($query as $post) :
the_post();
endforeach;

问题是, 如何获取$ post [$ i], 以便获得以下结果。

<!-- i want to print wordpress post like this format-->

<div class='grid'>

  <div class='column'>
  <!-- $i = 0 -->
  <!-- start loop -->
  <!-- print post -->
  <span><?php echo $post[$i]->title; ?></span>
  <!-- $i = $i + 3 -->
  <!-- end loop -->
  </div>

</div>

这个概念是我想根据$ i的值显示第一个帖子, 然后根据$ i = $ i + 3的值显示下一个帖子, 因此最终结果将显示$ post [0], $ post [ 3], $ post [6], […], 有什么建议吗?

对不起, 我的英语, 谢谢-埃德温。


#1


这应该是显示你的帖子的解决方案:

<?php

    $posts = new WP_Query( $args );

    if( $posts->have_posts() ) : //checks if query have posts
        while( $posts->have_posts() ) : $posts->the_post();
            if( $i % 3 == 0 ) : //every third post ?>
                <div class='grid'>
                    <div class='column-<?php the_ID(); ?>'>
                        <span>the_title();</span>
                    </div>
                </div>
            <?php endif;
            $i++;
        endwhile;
    else :
        // if no posts
    endif;
    wp_reset_postdata();

?>

#2


我从其他问题和其他论坛得到了这个答案

$query = new WP_Query( array( 'post_type' => 'page' ) );
$posts = $query->posts;

foreach($posts as $post) {
    // Do your stuff, e.g.
    // echo $post->post_name;
}

我对代码进行了一些更改以获得我的结果

$query = new WP_Query();
$posts = $query->posts;

$i = 0;
foreach($posts as $post) {
    // Do your stuff, e.g.
    // echo $post[$i]->post_name;
$i = $i + 3;
}
赞(0)
未经允许不得转载:srcmini » 如何打印wordpress帖子?

评论 抢沙发

评论前必须登录!