How to split data output of a custom post type loop

How do I split the values within a custom post type loop so that the title is in the first loop which outputs in the first DIV and the content is in the second loop outputting in the second DIV? Two loops may not be the best way to do, I don’t know. Somebody mentioned to me about one loop then storing in a string and then splitting the string. I have no idea how to do this either.

I also need a new row to begin once the number of posts reach 5.

Read More

Here’s my PHP loop…

<?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;
?>

The HTML output i’m looking for as follows…

<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>

Pseudo Code…

$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;
    }
}

Related posts

1 comment

  1. can you plz try this code.

    <?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>";
    }
    ?>
    

    it will give out put see below.

    <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>
    

Comments are closed.