Initialize the array into while recent post woocommerce?
When I want to display the latest product
I want to put in an array and show them in one section
only 9 post Initialize.
correct?
<?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; ?>
You're on the right track. Does this help?
I prefer to change the array()
to shorthand: []
.
<?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>';
}
}?>