'post', 'posts_per_page' => $news_posts_count, 'orderby' => $news_order_by, 'order' => $news_order, 'post_status' => 'publish', 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ) ) ); // 如果指定了分类ID,添加分类过滤 if (!empty($news_category_id)) { $query_args['cat'] = $news_category_id; } // 查询文章 $news_query = new WP_Query($query_args); // 获取随机特色图像和对应的文章链接 $featured_image_url = ''; $featured_image_link = ''; if ($news_query->have_posts()) { // 获取所有文章的特色图像和链接 $featured_data = array(); while ($news_query->have_posts()) { $news_query->the_post(); if (has_post_thumbnail()) { $featured_data[] = array( 'image' => get_the_post_thumbnail_url(get_the_ID(), 'full'), 'link' => get_permalink() ); } } // 随机选择一张图片和对应链接 if (!empty($featured_data)) { $random_item = $featured_data[array_rand($featured_data)]; $featured_image_url = $random_item['image']; $featured_image_link = $random_item['link']; } // 重置查询 wp_reset_postdata(); } // 如果没有找到特色图像,使用默认图片 if (empty($featured_image_url)) { $featured_image_url = 'http://nh.matepress.cn/wp-content/uploads/2025/06/logo.svg'; } ?>

have_posts()) : while ($news_query->have_posts()) : $news_query->the_post(); // 获取文章分类 $categories = get_the_category(); $category_name = !empty($categories) ? $categories[0]->name : 'News'; ?>
暂无文章
提示