[英]how can i get array in proper format from this foreach and nested if condition in php?
[英]how can i get proper output from this array
我在wordpress上使用自定義帖子,我試圖將我想要的輸出作為一個數組。但它沒有顯示任何內容
代碼在這里:
$idx = 0;
$wp_query = new WP_Query(array( 'post_type' => 'book-products',
'posts_per_page' => -1,
'post_status' => array('publish'),
'tax_query' => array(
'relation' => 'AND',
array( 'taxonomy' => 'book-collection',
'field' => 'slug',
'terms' => $term),
array( 'taxonomy' => 'book-style',
'field' => 'slug',
'terms' => $style,
'operator' => 'NOT IN')
)
)
);
while ($wp_query->have_posts()) : $wp_query->the_post();
$id = get_the_ID();
$color_t = wp_get_post_terms(get_the_ID(), 'book-product-color');
$color = $color_t[0]->name;
$color_code = strtoupper($color_t[0]->slug);
if((is_array($output) && !array_key_exists($idx, $output)) || $output[$idx]['name'] != $style) {
//if(!array_key_exists($idx, $output) || $output[$idx]['name'] != $style) {
$idx++;
$output[$idx] = array('name' => $style);
}
$output[$idx]['products'][] = array( 'id' => get_the_ID(),
'style' => $style,
'title' => get_the_title(),
'color' => $color,
'size' => $size,
'lens' => $lens,
'thumb_img' => wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'medium')[0],
'front_img' => wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'full')[0],
'profile_img' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'profile-image', get_the_ID(), 'full'),
'stacked_img' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'stacked-image', get_the_ID(), 'large'),
'cropped-front-image' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'cropped-front-image', get_the_ID(), 'full'),
'cropped-side-image' => MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'cropped-side-image', get_the_ID(), 'full'),
'content' => wp_strip_all_tags(get_the_content()),
'post_type' => get_post_type());
endwhile;
//trying to output product data.need to get the ID as an example
foreach($output as $value)
{
echo $value['id'];
}
我也嘗試將數據顯示為json但不起作用。 但是,如果我將數據檢查為var_dumb,那么我可以在那里看到所有數據。 我不是多維數組的專家,所以我確信存在一些問題。
你的id
是$idx
里面的products
。
$ids = array_column($output, 'id');
foreach ( $ids as $id ) {
echo $id;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.