将数组初始化为最近发布的 woocommerce



将数组初始化为最近的帖子woocommerce?

当我想展示最新产品时

我想放入一个数组并在一个部分中显示它们

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

你走在正确的轨道上。这有帮助吗?

我更喜欢将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>';
    }
}?>

最新更新