当前分类页面 > 自定义器设置 * * 使用方法: * - 在分类页面使用:[nenghui_news_grid] (自动显示当前分类文章) * - 指定分类使用:[nenghui_news_grid category_id="5"] (显示分类ID为5的文章) * - 指定多个分类:[nenghui_news_grid category_id="5,6,7"] (显示分类ID为5、6、7的文章) * - 显示所有文章:[nenghui_news_grid category_id=""] (覆盖当前分类,显示所有文章) */ // 防止直接访问 if (!defined('ABSPATH')) { exit; } // 检查是否通过短代码调用 global $news_shortcode_atts; // 确保 $news_shortcode_atts 是数组 if (!is_array($news_shortcode_atts)) { $news_shortcode_atts = array(); } // 获取自定义器设置,如果有短代码参数则优先使用短代码参数 $news_title = !empty($news_shortcode_atts['title']) ? $news_shortcode_atts['title'] : get_theme_mod('news_title', 'NEWS'); // 分类ID优先级:短代码参数 > 当前分类页面 > 自定义器设置 $news_category_id = ''; $news_tag_id = ''; $news_author_id = ''; if (!empty($news_shortcode_atts['category_id'])) { // 优先使用短代码指定的分类ID $news_category_id = $news_shortcode_atts['category_id']; } elseif (is_category()) { // 如果是分类页面,使用当前分类ID $news_category_id = get_queried_object_id(); } elseif (is_tag()) { // 如果是标签页面 $news_tag_id = get_queried_object_id(); } elseif (is_author()) { // 如果是作者页面 $news_author_id = get_queried_object_id(); } else { // 否则使用自定义器设置的分类ID $news_category_id = get_theme_mod('news_category_id', ''); } $news_order_by = !empty($news_shortcode_atts['order_by']) ? $news_shortcode_atts['order_by'] : get_theme_mod('news_order_by', 'date'); $news_order = !empty($news_shortcode_atts['order']) ? $news_shortcode_atts['order'] : get_theme_mod('news_order', 'DESC'); // 检查是否启用分页 $enable_pagination = !empty($news_shortcode_atts['enable_pagination']) ? wp_validate_boolean($news_shortcode_atts['enable_pagination']) : get_theme_mod('news_enable_pagination', false); // 根据是否启用分页设置文章数量 if ($enable_pagination) { $news_posts_count = !empty($news_shortcode_atts['posts_per_page']) ? intval($news_shortcode_atts['posts_per_page']) : get_theme_mod('news_posts_per_page', 6); } else { $news_posts_count = !empty($news_shortcode_atts['posts_count']) ? intval($news_shortcode_atts['posts_count']) : get_theme_mod('news_posts_count', 12); // 确保文章数量为12(3行4列) if ($news_posts_count < 12) { $news_posts_count = 12; } } // 设置容器ID和CSS类 $container_id = !empty($news_shortcode_atts['id']) ? $news_shortcode_atts['id'] : 'nenghui-news-block'; $container_class = !empty($news_shortcode_atts['class']) ? $news_shortcode_atts['class'] : ''; // 获取当前页码 $current_page = max(1, get_query_var('paged', 1)); if (!$current_page) { $current_page = max(1, get_query_var('page', 1)); } // 构建查询参数 $query_args = array( 'post_type' => 'post', 'posts_per_page' => $news_posts_count, 'orderby' => $news_order_by, 'order' => $news_order, 'post_status' => 'publish' ); // 如果启用分页,添加分页参数 if ($enable_pagination) { $query_args['paged'] = $current_page; } // 如果指定了分类ID,添加分类过滤 if (!empty($news_category_id)) { // 支持多分类(逗号分隔),清理可能存在的空格 if (strpos($news_category_id, ',') !== false) { $news_category_id = implode(',', array_map('trim', explode(',', $news_category_id))); } $query_args['cat'] = $news_category_id; } // 如果是标签页面 if (!empty($news_tag_id)) { $query_args['tag_id'] = $news_tag_id; } // 如果是作者页面 if (!empty($news_author_id)) { $query_args['author'] = $news_author_id; } if (is_archive() && !is_category() && !is_tag() && !is_author()) { // 处理其他类型的存档页面(如日期存档) // 通常 WP_Query 会自动处理,但在自定义查询中我们需要显式处理或者不限制分类 } // 查询文章 $news_query = new WP_Query($query_args); ?>
Please add some posts to display in this block.