'', 'posts_count' => 4, 'title' => '新闻资讯', 'subtitle' => 'NEWS' ); $args = wp_parse_args($args ?? array(), $defaults); // 构建查询参数 $query_args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $args['posts_count'], 'orderby' => 'date', 'order' => 'DESC', // 移除特色图片强制要求,允许显示没有特色图片的文章 // 'meta_query' => array( // array( // 'key' => '_thumbnail_id', // 'compare' => 'EXISTS' // ) // ) ); // 如果指定了分类,添加分类查询 if (!empty($args['category'])) { if (is_numeric($args['category'])) { // 使用分类ID $query_args['cat'] = intval($args['category']); } else { // 使用分类别名 $query_args['category_name'] = sanitize_text_field($args['category']); } // 调试信息:记录分类查询参数 if (defined('WP_DEBUG') && WP_DEBUG) { error_log('News Block Category Query: ' . print_r(array( 'category_param' => $args['category'], 'is_numeric' => is_numeric($args['category']), 'query_args' => $query_args ), true)); } } // 尝试获取缓存 $cache_key = 'news_block_' . md5(serialize($query_args) . (function_exists('get_theme_cache_version') ? get_theme_cache_version() : '')); $posts = get_transient($cache_key); if ($posts === false) { // 执行查询 $news_query = new WP_Query($query_args); $posts = $news_query->posts; // 如果有文章,设置缓存(缓存1小时) if (!empty($posts)) { set_transient($cache_key, $posts, 3600); } } // 如果没有文章,显示提示信息 if (empty($posts)) { wp_reset_postdata(); echo '

' . esc_html($args['subtitle']) . '

' . esc_html($args['title']) . '

暂无相关文章

'; return; } // 获取文章数组 $featured_post = $posts[0] ?? null; $other_posts = array_slice($posts, 1, 3); ?>

ID)); ?>

ID); if ($excerpt) : ?>

ID)); ?> ID, 'post_views_count', true); if ($post_views) : ?>
ID)) : ?> ID, 'news-medium', array( 'alt' => esc_attr(get_the_title($post->ID)) )); ?>
暂无图片

ID)); ?>

ID)); ?> ID, 'post_views_count', true); if ($post_views) : ?>