2015-06-12 31 views
-2

我在wordpress中有一個自定義函數,下面的代碼用於顯示自定義分類的縮略圖。我想實現的是,我想在每六張圖像後動態插入一個div class="row"。我如何去做這件事?在wordpress中動態插入div中的div

   $max = 10; //number of categories to display 
       $taxonomy = 'cp_recipe_category'; 
       $terms = get_terms($taxonomy, 'hide_empty=1'); 

       // Random order 
       shuffle($terms); 

       // Get first $max items 
       $terms = array_slice($terms, 0, $max); 

       // Sort by name 
       usort($terms, function($a, $b){ 
        return strcasecmp($a->name, $b->name); 
       }); 

       // Echo random terms sorted alphabetically 
       if ($terms) { 
        foreach($terms as $term) { 
         echo'<div class="vc_col-sm-2 wpb_column vc_column_container ">'; 
         echo'<img src="'; echo z_taxonomy_image_url($term->term_id); echo'" />'; 
         echo '<p><a href="' .get_term_link($term, $taxonomy) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name.'</a> 
         </p> '; 
         echo'</div>'; 


        } 
       } 

回答

1

有點像?

  $max = 10; //number of categories to display 
      $taxonomy = 'cp_recipe_category'; 
      $terms = get_terms($taxonomy, 'hide_empty=1'); 

      // Random order 
      shuffle($terms); 

      // Get first $max items 
      $terms = array_slice($terms, 0, $max); 

      // Sort by name 
      usort($terms, function($a, $b){ 
       return strcasecmp($a->name, $b->name); 
      }); 

      // Echo random terms sorted alphabetically 
      if ($terms) { 
       $i = 0; 
       foreach($terms as $term) { 
        $i++; 
        echo'<div class="vc_col-sm-2 wpb_column vc_column_container ">'; 
        echo'<img src="'; echo z_taxonomy_image_url($term->term_id); echo'" />'; 
        echo '<p><a href="' .get_term_link($term, $taxonomy) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name.'</a> 
        </p> '; 
        echo'</div>'; 
        if($i%6 == 0) { echo '<div class="row"></div>'; } 


       } 
      } 
0

這裏是你如何能做到這一點:

$r = 0; 
foreach($terms as $term) { 

    if($r ==5) 
     { 
     //add your div 
     $r = 0; 
     } 
    else 
     $r++; 
    } 
0

試試這個:

$i = 1; 
//added before to ensure it gets opened 
echo '<div>'; 
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); 
    // post stuff... 

    // if multiple of 3 close div and open a new div 
    if($i % 3 == 0) {echo '</div><div>';} 

$i++; endwhile; endif; 
//make sure open div is closed 
echo '</div>';