[英]Selecting x amount of items from a for each loop in php?
I dont know if this is possible, but im trying to get pagination from a get_pages()
function of Wordpress since i need those pages being echo with different classes or inside some div so I can manage them with Javascript later to match the desire template. 我不知道这是否可能,但是我试图从Wordpress的
get_pages()
函数获取分页,因为我需要这些页面与不同类或在某些div内回显,因此我以后可以使用Javascript管理它们以匹配所需模板。
So far I have this to echo the content: 到目前为止,我有这个要回应的内容:
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$i =0;
foreach($pages as $page)
{
$content = $page->post_content;
if(!$content)
continue;
$i++;
$content = apply_filters('the_content', $content);
$doc = new DOMDocument();
$doc->loadHTML($content);
$xpath = new DOMXPath($doc);
$imagesrc = $xpath->evaluate("string(//img/@src)");
$imagehref = $xpath->evaluate("string(//a/@href)");
for($i = 0; $i < count($content); $i+=5) {
echo "<li><a id='item-$i' href='$imagehref'><img src='$imagesrc'/></a></li>\n";
for($i = 5; $i < count($content); $i+=18) {
echo "more";
}
}
So right now i do get all items no matter if there is 10 or 50, but what i want it is to put them in blocks of 8, is that possible to do inside a for each? 所以现在我确实得到所有项目,不管有10个还是50个,但是我想要将它们分成8个块,是否有可能在每个内部进行? or there is another way to do it?
还是有另一种方法?
Note I need to plit large set of posts into blocks of 8 注意 我需要将大量的帖子分成8个块
any help will be really appreciated thanks :) 任何帮助将不胜感激谢谢:)
An alternative to @aelfric5578's answer is to use array_chunk
to break the array into the pieces you need. @ aelfric5578答案的替代方法是使用
array_chunk
将数组分解成所需的片段。 You could break the original $pages
array or generate an array in the foreach
and break that, whichever works best for you but I'd probably break the $pages
array. 您可以破坏原始的
$pages
数组或在foreach
生成一个数组并将其破坏,以最适合您的方法为准,但我可能会破坏$pages
数组。 Here is an example using the original $pages
. 这是使用原始
$pages
的示例。
$blocks = array_chunk($pages, 8);
The benefit is going to be readability and maintainability. 好处将是可读性和可维护性。 Since you already have an iterator-- and maybe should have maybe two, you seem to be using the same variable name twice-- it could get confusing to add more counters.
由于您已经有一个迭代器-也许应该有两个,所以您似乎两次使用了相同的变量名-添加更多计数器可能会造成混淆。
You need a separate counter. 您需要一个单独的柜台。 Since you already have
$i
you can use a conditional and the modulo operator. 由于您已经有了
$i
,因此可以使用条件运算符和取模运算符。 $i % 8
returns zero if $i
is divisible by 8 and the remainder of that division otherwise. 如果
$i
被8整除,则$i % 8
返回零,否则除以该除法的其余部分。
So, if $i % 8 == 0
, start your grouping. 因此,如果
$i % 8 == 0
,则开始分组。 Assuming you plan on grouping things with a <div>
or something like that. 假设您计划使用
<div>
或类似的东西对事物进行分组。 You can close your grouping after $i % 8 == 7
at the end of your for loop. 您可以在for循环结束时在
$i % 8 == 7
之后关闭分组。
However, it seems you're setting $i
twice. 但是,似乎您两次设置了
$i
。 Is there a reason for this? 是否有一个原因? For my solution to work, you would need to use a different variable for the inner for loop.
为了使我的解决方案有效,您需要为内部for循环使用其他变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.