2017-10-14 2 views
0

最近のポストwoocommerceの間に配列を初期化しますか?最近のポストwoocommerce中に配列を初期化しますか?

私は最新の製品

を表示したい場合は、私は、配列に入れ、1つのセクション

のみ後9初期化でそれらを表示したいです。

正しいですか?

<?php 
$arry = array("", "", "","", "", "","", "", ""); 
$params = array('posts_per_page' => 5,'post_type' => 'product'); 
$wc_query = new WP_Query($params); 
?> 
<?php if ($wc_query->have_posts()) : ?> 
<?php $i =0 ; while ($wc_query->have_posts()) : $wc_query->the_post(); ?> 

<?php $array[$i] = the_title(); ?> 

<?php $i++; endwhile; ?> 
<?php endif; ?> 

答えて

1

あなたは正しい方向です。これは役に立ちますか?

array()を簡略化することをお勧めします。[]です。

<?php 
$arry = array(); 
$params = array('posts_per_page' => 9,'post_type' => 'product'); 
$wc_query = new WP_Query($params); 
?> 
<?php if ($wc_query->have_posts()) : ?> 
<?php $i =0 ; while ($wc_query->have_posts()) : $wc_query->the_post(); ?> 

<?php $array[$i] = array('index'=>$i,'title'=>the_title(),'content'=>the_content(),'permalink'=>the_permalink()); ?> 

<?php $i++; endwhile; ?> 
<?php endif; ?> 

<?php if(!empty($array)){ 
    foreach ($array as $post) { 
     echo '<h2>'.$post['title'].'</h2>'; 
     echo '<p>'.$post['content'].'</p>'; 
    } 
}?> 
関連する問題