简体   繁体   English

带有类别和帖子的Wordpress复杂循环

[英]Wordpress complex loop with categories and posts

First time question, long time reader. 初次提问,长期读者。 I couldn't find any answers to this, or maybe i'm not quite searching correctly. 我找不到任何答案,或者也许我搜索不正确。 Hopefully you can help me. 希望你能帮助我。

I have three categories. 我分为三类。 Each category has an unlimited amount of posts. 每个类别都有无限数量的帖子。 I only need to display the title and the date. 我只需要显示标题和日期。

The problem is that I want to loop one post at a time from each category. 问题是我想一次从每个类别中循环发布一个帖子。 The posts must be ordered by date. 帖子必须按日期排序。

A very basic example of the output i'm trying to achieve: 我正在尝试实现的输出的一个非常基本的示例:

  • Category 1: Post one title and date 类别1:张贴一个标题和日期
  • Category 2: Post one title and date 类别2:发布一个标题和日期
  • Category 3: Post one title and date 类别3:发布一个标题和日期
  • Category 1: Post two title and date 类别1:张贴两个标题和日期
  • Category 2: Post two title and date 类别2:张贴两个标题和日期
  • Category 3: Post two title and date 类别3:张贴两个标题和日期
  • Category 1: Post three title and date 类别1:张贴三个标题和日期
  • Category 2: Post three title and date 第2类:发布三个标题和日期
  • Category 3: Post three title and date 类别3:发布三个标题和日期

and so on... 等等...

I hope this doesn't sound to confusing. 我希望这不会让人感到困惑。 Thanks for your time. 谢谢你的时间。

One possibility is to simply create three sets of get_posts(). 一种可能性是简单地创建三组get_posts()。 One of them is set to category 1, the others are 2 and 3 respectively. 其中一个设置为类别1,其他分别设置为2和3。 From there you can set up a loop using a few foreach statements that store the title and date into a multidimensional array that you iterate through and echo a moment later. 在这里,您可以使用一些foreach语句来建立循环,这些语句将标题和日期存储在多维数组中,您可以循环访问并稍后回显。

If that was a bit hard to follow: 如果那很难遵循:

Lets assume that we have already selected the three get_post( $args ) where $cat1, $cat2 and $cat3 are the resulting posts. 假设我们已经选择了三个get_post($ args),其中$ cat1,$ cat2和$ cat3是结果帖子。 $c starts as 0 for this. $ c为此开始为0。

foreach ($cat1 as $post) : setup_postdata($post);
  $cat1Array[$c] = the_title( false, false, false ) . the_date( false, false, false, false );
  $c++;
endforeach

Which is repeated twice to give $cat2Array and $cat3Array: 重复两次,得到$ cat2Array和$ cat3Array:

for ($c=0; $c<=<iterations>; $c++) {
  echo $cat1Array[$c];
  echo $cat2Array[$c];
  echo $cat3Array[$c];
}

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

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