Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将每3篇文章包装在一个包含类别名称的div中?_Php_Html_Wordpress - Fatal编程技术网

Php 如何将每3篇文章包装在一个包含类别名称的div中?

Php 如何将每3篇文章包装在一个包含类别名称的div中?,php,html,wordpress,Php,Html,Wordpress,我正在使用WordPress,我想把每3篇文章都放在一个div中。这应该很简单,但为了让事情更复杂,我还想在这些div中列出类别的名称 我的代码: <?php $i = 1; $taxonomy = 'category'; $param_type = 'category__in'; $term_args=array( 'orderby' => 'name', 'order' => 'DESC', 'child_of' => 4 ); $terms

我正在使用WordPress,我想把每3篇文章都放在一个div中。这应该很简单,但为了让事情更复杂,我还想在这些div中列出类别的名称

我的代码:

<?php
$i = 1;
$taxonomy = 'category';
$param_type = 'category__in';
$term_args=array(
  'orderby' => 'name',
  'order' => 'DESC',
  'child_of'      => 4
 );
$terms = get_terms($taxonomy,$term_args);
echo '<div>';
if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<h1>' . $term->name . '</h1>' ;
      while ($my_query->have_posts()) : $my_query->the_post();
       if($i % 3 == 0) {echo '</div><div>';} ?>
      <a>"> <?php the_post_thumbnail(); ?> </a>
       <?php
   $i++;
      endwhile;
    }
  }
}
echo '</div>';
wp_reset_query();
?>

">  
我的预期结果是:

<div>
<h1>Category01</h1>
<a><img /></a>
<a><img /></a>
</div>

<div>
<a><img /></a>
<a><img /></a>
<h1>Category02</h1>
</div>

<div>
<a><img /></a>
<a><img /></a>
<a><img /></a>
</div> // etc.

类别01
类别02
//等等。

我的问题是,如果我在foreach循环中关闭div,它只会在3个
标记后关闭,但是如果我在while循环中关闭div,它会在3个图像后关闭,但是
标记不计入3个元素中。有什么帮助吗?我现在可以用JavaScript解决它,但我更愿意让它在服务器上工作好的,这是一个工作代码:

<?php
$taxonomy = 'category';
$param_type = 'category__in';
$term_args=array(
  'orderby' => 'name',
  'order' => 'DESC',
  'child_of'      => 4,
 );
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
  $i = 0;
  echo "<div>\n";
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
       $h1 = '<h1>' . $term->name . '</h1>' ;
      while ($my_query->have_posts()) : $my_query->the_post();
         if ($h1) {
            if (++$i % 3 == 1 && $i > 1) {
               echo "</div><div>\n";
            }
            echo $h1;
            $h1 = '';
         }
         if(++$i % 3 == 1) {echo "</div><div>\n";} ?>
         <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?> </a>
      <?php
      endwhile;
     }
  }
  echo "</div>\n";
}
wp_reset_query();
?>

这可能是最简单的方法: