WordPress批量循环输出分类列表页代码
下面是上半段代码:
<?php
/**
* 描述:展会分类区块美化升级版,全部圆角移除,直角样式
*/
if (!defined('ABSPATH')) {
exit;
}
get_header();
// ====================== 配置区【只需在这里修改分类】======================
$expo_categories = [
['cat_id' => 1, 'name' => '交通/航空/海洋'],
['cat_id' => 138, 'name' => '农林/畜牧/渔业'],
['cat_id' => 126, 'name' => '医药/健康/环保'],
['cat_id' => 125, 'name' => '地产/建筑/家居'],
['cat_id' => 137, 'name' => '安防/劳保/消防'],
['cat_id' => 136, 'name' => '工业/机械/仪表'],
['cat_id' => 129, 'name' => '文化/教育/体育'],
['cat_id' => 124, 'name' => '电子/信息/通讯'],
['cat_id' => 130, 'name' => '礼品/广告/包装'],
['cat_id' => 135, 'name' => '穿戴/服装/纺织'],
['cat_id' => 133, 'name' => '美容/休闲/人生'],
['cat_id' => 134, 'name' => '能源/矿产/化工'],
['cat_id' => 131, 'name' => '艺术/设计/时尚'],
['cat_id' => 127, 'name' => '车类/旅游/酒店'],
['cat_id' => 128, 'name' => '金融/贸易/商业'],
['cat_id' => 132, 'name' => '食品/饮料/烘培'],
['cat_id' => 144, 'name' => '其它展会'],
];
/**
* 渲染单个展会分类区块【封装函数,消除重复代码】
* @param int $cat_id 分类ID
* @param string $cat_name 分类名称
*/
function render_expo_category_block(int $cat_id, string $cat_name): void
{
$args = array(
'cat' => $cat_id,
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$query = new WP_Query($args);
$cat_link = get_category_link($cat_id);
?>
<div class="Exhibition-Category-1">
<div class="expo-cat-header">
<h3 class="expo-cat-title">
<a href="<?php echo esc_url($cat_link); ?>"><?php echo esc_html($cat_name); ?></a>
</h3>
<a class="expo-more-link" href="<?php echo esc_url($cat_link); ?>">查看全部 <i class="fa fa-angle-right"></i></a>
</div>
<div class="expo-grid-list">
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post();
$img_url = get_random_expo_default_img();
$start_time = get_post_meta(get_the_ID(), 'Exhibition_start_time', true);
$end_time = get_post_meta(get_the_ID(), 'Exhibition_end_time', true);
$city = get_expo_meta(get_the_ID(), 'Exhibition_City');
$date_text = get_expo_date_range($start_time, $end_time);
$status_text = get_expo_status_text($start_time, $end_time);
$view_num = getPostViews(get_the_ID());
?>
<div class="expo-card">
<div class="post-thumbnail">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium_large', ['loading' => 'lazy', 'alt' => get_the_title()]); ?>
</a>
<?php else : ?>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo esc_url($img_url); ?>" alt="<?php the_title_attribute(); ?>" loading="lazy">
</a>
<?php endif; ?>
<div class="exhibition-time-badge">
<?php echo esc_html($status_text); ?>
</div>
</div>
<div class="post-card-content">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="post-meta">
<span>📅 <?php echo esc_html($date_text); ?></span>
<span>👁 <?php echo esc_html($view_num); ?>阅读 评论关</span>
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<p style="grid-column:1/-1;text-align:center;padding:60px 0;color:#888;font-size:16px;">该分类下暂无展会文章</p>
<?php endif; ?>
</div>
</div>
<?php
}
// ====================== 配置区结束 ======================
?>
方法一 下面的是下半段代码放到要显示位置:
<?php
// 循环输出所有展会分类区块
foreach ($expo_categories as $item) {
render_expo_category_block($item['cat_id'], $item['name']);
}
?>
方法二 下面的是下半段代码放到要显示位置:插入广告图片(这广告是使用插件)
<?php
// 首页横幅广告位:按出现顺序依次取用
$banner_slots = [
'home_banner_1',
'home_banner_2',
'home_banner_3',
'home_banner_4',
'home_banner_5',
'home_banner_6',
];
$banner_interval = 2; // 每隔几个分类插一次广告
$banner_index = 0; // 当前取到第几个广告位
foreach ($expo_categories as $i => $cat) {
// 1. 渲染展会分类区块
render_expo_category_block((int) $cat['cat_id'], (string) $cat['cat_name']);
// 2. 每 3 个分类之后插入一个横幅广告
if (($i + 1) % $banner_interval === 0 && $banner_index < count($banner_slots)) {
$slot = $banner_slots[$banner_index];
if (function_exists('lzsa_ad')) {
echo '<div class="home-banner-slot home-banner-slot--' . ($banner_index + 1) . '">';
lzsa_ad($slot); // 依次输出 首页横幅 1~5
echo '</div>';
}
$banner_index++;
}
}
?>
