WordPress : Comment diviser la sortie de données d’une boucle de type de publication personnalisée

Publié par Jean-Michel le

Comment diviser les valeurs dans une boucle de type de publication personnalisée afin que le titre se trouve dans la première boucle qui sort dans le premier DIV et que le contenu se trouve dans la deuxième boucle qui sort dans le deuxième DIV ? Deux boucles ne sont peut-être pas la meilleure façon de faire, je ne sais pas. Quelqu’un m’a parlé d’une boucle puis de la stocker dans une chaîne, puis de la diviser. Je n’ai aucune idée de comment faire cela non plus.

J’ai également besoin d’une nouvelle ligne pour commencer une fois que le nombre de messages atteint 5.

Voici ma boucle 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;
?>

La sortie HTML que je recherche est la suivante…

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

Pseudocode…

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

Solution n°1 trouvée

pouvez-vous svp essayer ce 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>";
}
?>

il donnera mis voir ci-dessous.

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

Solution n°2 trouvée

pouvez-vous svp essayer ce 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>";
}
?>

il donnera mis voir ci-dessous.

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

Catégories : Wordpress

Jean-Michel

Jean-Michel est en charge de la partie blog du site. Il met en place la stratégie de contenu et répond aux questions fréquentes sur Wordpress.

0 commentaire

Laisser un commentaire

Avatar placeholder

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *