如何拆分自定义帖子类型循环的数据输出

时间:2015-05-20 09:29:54

标签: php wordpress for-loop split wordpress-theming

如何在自定义帖子类型循环中拆分值,以便标题位于第一个DIV中输出的第一个循环中,而内容位于第二个DIV输出的第二个循环中?我不知道,两个循环可能不是最好的方法。有人向我提到一个循环,然后存储在一个字符串中然后拆分字符串。我也不知道怎么做。

一旦帖子数达到5,我还需要一个新行。

这是我的PHP循环...

<?php
  $custom_query = new WP_Query( $custom_args );
  query_posts(array( 'post_type' => array('team')) ); 

  if ( $custom_query->have_posts() ) : 
  /* Start my loop */
    while ( $custom_query->have_posts() ) : $custom_query->the_post();
      echo "<div>". the_title(); . "</div>";
      echo "<div>". the_content(); . "</div>";
    endwhile;
  endif;
?>

HTML输出我正在寻找如下......

<div class"row">
  <div class="container">
    <div class="title chris"></div>
    <div class="title darren"></div>
    <div class="title ryan"></div>
    <div class="title john"></div>
    <div class="title emma"></div>
  </div>
  <div class="container">
    <div class="title chris"></div>
    <div class="title darren"></div>
    <div class="title ryan"></div>
    <div class="title john"></div>
    <div class="title emma"></div>
  </div>
</div>
<div class"row">
  <div class="container">
    <div class="title chris"></div>
    <div class="title darren"></div>
    <div class="title ryan"></div>
    <div class="title john"></div>
    <div class="title emma"></div>
  </div>
  <div class="container">
    <div class="title chris"></div>
    <div class="title darren"></div>
    <div class="title ryan"></div>
    <div class="title john"></div>
    <div class="title emma"></div>
  </div>
</div>
<div class"row">
  <div class="container">
    <div class="title chris"></div>
    <div class="title darren"></div>
    <div class="title ryan"></div>
    <div class="title john"></div>
    <div class="title emma"></div>
  </div>
  <div class="container">
    <div class="title chris"></div>
    <div class="title darren"></div>
    <div class="title ryan"></div>
    <div class="title john"></div>
    <div class="title emma"></div>
  </div>
</div>

伪代码......

$team; // array
$avatar = '';
$memberinfo = '';
foreach ($team as $member){
    $avatar .= '<div>'. $member->avatar .'</div>';
    $memberinfo .= '<div class="content">'. $member->memberinfo .'</div>';

    if($newRow){
        echo $avatar;
        echo $memberinfo;
    }
}

1 个答案:

答案 0 :(得分:1)

你可以试试这段代码。

<?php
/* To get current page from query var */
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => array('post'),'posts_per_page' => -1,'paged' => $paged);

 /* Create custom WP_Query */
$custom_query = new WP_Query( $args );
$post_ids = array();
if ( $custom_query->have_posts() ) {
    /* the wordpress loop */
    while ( $custom_query->have_posts() ) : $custom_query->the_post();
        /* Start my loop to get all post id in one array called "$post_ids" */
        $post_ids[] = get_the_id();
    endwhile;
    /* end of the loop */
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();


$count = 1;
$post_title = $post_content = '';
foreach( $post_ids as $post_id ) 
{
    if ($count%5 == 1)
    {  
         echo '<div class="row">';
    }
    $post_title .= '<div class="title">'.get_the_title($post_id).'</div>';
    $post_content .= '<div class="content">'.get_post_field('post_content', $post_id).'</div>';
    if ($count%5 == 0)
    {
        echo $post_title;
        echo  $post_content;
        echo "</div>";
        $post_title = $post_content = '';
    }
    $count++;
}
if ($count%5 != 1) {
    echo $post_title;
    echo  $post_content;
    echo "</div>";
}
?>

它会给出看看下面的内容。

<div class="row">
    <div class="title">title1</div>
    <div class="title">title2</div>
    <div class="title">title3</div>
    <div class="title">title4</div>
    <div class="title">title5</div>
    <div class="content">content1</div>
    <div class="content">content2</div>
    <div class="content">content3</div>
    <div class="content">content4</div>
    <div class="content">content5</div>
</div>
<div class="row">
    <div class="title">title6</div>
    <div class="title">title7</div>
    <div class="title">title8</div>
    <div class="title">title9</div>
    <div class="title">title10</div>
    <div class="content">content6</div>
    <div class="content">content7</div>
    <div class="content">content8</div>
    <div class="content">content9</div>
    <div class="content">content10</div>
</div>
<div class="row">
    <div class="title">title11</div>
    <div class="title">title12</div>
    <div class="title">title13</div>
    <div class="title">title14</div>
    <div class="title">title15</div>
    <div class="content">content11</div>
    <div class="content">content12</div>
    <div class="content">content13</div>
    <div class="content">content14</div>
    <div class="content">content15</div>
</div>