简体   繁体   English

WordPress自定义循环帖子

[英]Wordpress custom loop posts

I want to make a post page in the category.php template. 我想在category.php模板中创建一个帖子页面。 I want the first post to be a block of 600px by 600px and then 4 posts next to that of all 295px by 295px and the row under that first 4 blocks and than 1 big block etc. So it will look like this: 我希望第一个帖子是600px x 600px的块,然后是所有295px x 295px的块旁边的4个帖子,以及该前4个块下的行,而不是1个大块等。因此,它看起来像这样: 在此处输入图片说明

can this be achieved with the 可以做到这一点

<?php while (have_posts()) : the_post(); ?>

loop? 环?

Thanks. 谢谢。

Yeah, you can do this, but you'll want to look at get_posts instead. 是的,您可以执行此操作,但是您将需要查看get_posts Check out the examples there, they'll give you something to work with. 在此处查看示例,它们将为您提供一些帮助。

Yes. 是。 You just need to keep track of whether the big image is on the left or right, and have a count running to tell you which post you're on in the grid/row...and then a way to close out the div tags when the loop is over. 您只需要跟踪大图是在左侧还是右侧,并运行一次计数即可告诉您网格/行中的哪条信息,然后关闭div标签当循环结束时。 Something along these lines: 遵循以下原则:

<?php
$big_left = true;
$items_displayed_in_this_row = 0;
while( have_posts() ){
  if( $big_left ){
    if( 0 == $items_displayed_in_this_row ){
      // Code to put the big image on the left
      the_post();
    } else {
      // Code to put the small images on the right
      the_post();
    }
  } else {
    if( 4 == $items_displayed_in_this_row ){
      // Code to put the big image on the right
      the_post();
    } else {
      // Code to put the small images on the left
      the_post();
    }
  }
  $items_displayed_in_this_row++;
  if( 5 == $items_displayed_in_this_row ){
    if( $big_left ){
      $big_left = false;
    } else {
      $big_left = true;
    }
    $items_displayed_in_this_row = 0;
  }
}
// While loop just ended. Close out whatever divs might be open.
// eg, if $items_displayed_in_this_row > 0, you're in the middle of a row
// and should close it.

?>

No matter what, it's going to be long and ugly. 无论如何,这将是漫长而丑陋的。 But doing it in the main loop like this instead of a custom query will allow you to take advantage of pagination, prev/next, etc. 但是像这样在主循环中而不是自定义查询中进行操作,将使您能够利用分页,上一页/下一页等功能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM