我有以下PHP代码从特定类别中提取帖子并将其显示在无序列表中。
我想更改此内容,以便在一个<li>
中显示5个<ul>
,然后为另外5个<ul>
创建一个新的<?php
$args = array( 'posts_per_page' => 15, 'offset'=> 1, 'category' => $cat_ID );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
,依此类推。
这是我现有的代码:
int s = 45;
double d = *(double *) &s;
答案 0 :(得分:1)
另一种方法是使用array_chunk
,例如:
$myposts = [1,2,3,4,5,11,22,33,44,55,111,222,333,444];
foreach (array_chunk($myposts, 5) as $posts) {
echo "<ul>\n";
foreach ($posts as $post) {
echo '<li>' . $post. '</li>'. "\n";
}
echo "</ul>\n\n";
}
输出:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul>
<li>11</li>
<li>22</li>
<li>33</li>
<li>44</li>
<li>55</li>
</ul>
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
<li>444</li>
</ul>
答案 1 :(得分:0)
以下是每5次迭代使用modulus operator的粗略示例。
$myposts = array(1,2,3,4,5,6,7,8,9,10,11);
$output = '<ul>' . "\n";
foreach ($myposts as $count => $post ) {
if ($count > 1 && $count % 5 == 0 ) {
$output .= '</ul><ul>' . "\n";
}
$output .= '<li>' . $post . '</li>' . "\n" ;
}
rtrim($output, '</ul><ul>' . "\n"); //if it was the tenth iteration we don't want to open another ul
echo $output . '</ul>' . "\n";
输出:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul><ul>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul><ul>
<li>11</li>
</ul>