You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何统计WordPress自定义文章类型球员参与的赛事场次?

在球员单页面展示参赛赛事统计的实现方案

嘿,这个需求其实挺典型的,咱们一步步来搞定它。核心思路就是反向查询:从球员ID出发,找出所有关联了该球员的赛事(fixture),然后整理成统计表格展示出来。

准备工作确认

首先得确保你的ACF字段设置没问题:

  • 在fixture自定义文章类型中,你创建的关联字段(比如叫participating_players),类型是Post Object或Relationship,并且关联的文章类型选择了player。如果是多选球员,记得把字段的“允许选择多个值”打开。

实现步骤(模板代码版)

最直接的方式是修改player的单页面模板(single-player.php),把查询和统计逻辑加进去。以下是完整的代码示例,你可以根据自己的字段名调整:

<?php
// 获取当前球员的ID
$current_player_id = get_the_ID();

// 构建查询参数:找所有关联了当前球员的已发布赛事
$fixture_query_args = array(
    'post_type'      => 'fixture',
    'posts_per_page' => -1, // 显示所有赛事,如需分页可改成数字(比如10)
    'post_status'    => 'publish',
    'meta_query'     => array(
        array(
            'key'     => 'participating_players', // 替换成你ACF关联字段的实际名称
            'value'   => '"' . $current_player_id . '"', // 匹配序列化数组中的球员ID(避免部分匹配)
            'compare' => 'LIKE'
        )
    ),
    'orderby'        => 'date',
    'order'          => 'DESC' // 按赛事日期倒序,最新的在前
);

$player_fixtures = new WP_Query($fixture_query_args);

// 统计基础数据
$total_matches = $player_fixtures->found_posts;
$win_count = 0;
$lose_count = 0;
$draw_count = 0;
?>

<!-- 统计模块 -->
<div class="player-match-stats">
    <h3>参赛统计</h3>
    <div class="stats-summary">
        <span>总场次:<?php echo $total_matches; ?></span>
        <span>胜:<?php echo $win_count; ?></span>
        <span>平:<?php echo $draw_count; ?></span>
        <span>负:<?php echo $lose_count; ?></span>
    </div>

    <!-- 赛事表格 -->
    <?php if ($player_fixtures->have_posts()) : ?>
        <table class="matches-table">
            <thead>
                <tr>
                    <th>赛事名称</th>
                    <th>赛事日期</th>
                    <th>对阵双方</th>
                    <th>结果</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($player_fixtures->have_posts()) : $player_fixtures->the_post(); 
                    // 统计胜平负(如果你的赛事有结果字段)
                    $match_outcome = get_field('fixture_outcome'); // 替换成你的赛事结果字段
                    if ($match_outcome === 'win') $win_count++;
                    elseif ($match_outcome === 'draw') $draw_count++;
                    elseif ($match_outcome === 'lose') $lose_count++;
                ?>
                    <tr>
                        <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
                        <td><?php echo get_field('fixture_date'); ?></td> <!-- 替换成你的赛事日期字段 -->
                        <td><?php echo get_field('opponent_team'); ?></td> <!-- 替换成你的对手字段 -->
                        <td><?php echo ucfirst($match_outcome); ?></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    <?php else : ?>
        <p>该球员暂无参赛记录</p>
    <?php endif; ?>

    <?php wp_reset_postdata(); // 重置查询,避免影响后续内容 ?>
</div>

关键细节说明

  1. 查询逻辑:

    • 如果你的ACF关联字段是单选(Post Object不允许多选),可以把meta_query里的compare改成=,value直接用$current_player_id,不用加引号,效率更高。
    • 多选场景下用LIKE加引号,是因为ACF的Relationship字段会把选中的球员ID存储为序列化数组,比如a:2:{i:0;s:2:"12";i:1;s:2:"34";},用"12"可以精准匹配到对应的ID,避免和类似123的ID误匹配。
  2. 样式美化:
    上面的代码只提供了结构,你需要自己添加CSS来美化表格和统计模块,比如:

    .matches-table {width:100%; border-collapse:collapse; margin-top:1rem;}
    .matches-table th, .matches-table td {border:1px solid #eee; padding:0.8rem; text-align:left;}
    .matches-table th {background:#f5f5f5; font-weight:bold;}
    
  3. 性能优化:
    如果赛事数量很多,posts_per_page=-1可能会拖慢页面加载,建议改成分页(比如每页显示10条),或者只显示最近20场,再加一个“查看全部赛事”的链接到专门的归档页面。

备选方案:短代码版本

如果你不想修改主题模板,可以把逻辑封装成短代码,然后在球员的编辑页面(或自定义模板里)调用:

// 在主题的functions.php里添加这段代码
function player_fixture_stats_shortcode() {
    ob_start(); // 开启输出缓存

    // 把上面的查询和HTML输出代码复制到这里

    return ob_get_clean(); // 返回缓存内容
}
add_shortcode('player_matches', 'player_fixture_stats_shortcode');

之后在球员页面的编辑器里插入[player_matches]就能显示统计表格了。

内容的提问来源于stack exchange,提问作者aprestmo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:08:21