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

使用ACF Pro创建食谱后,如何在页面展示所有食谱摘要?

实现食谱分类管理与摘要展示的两种方案

嘿,我来帮你梳理下实现方法——其实你有两个主要方向可选,要不要改archive.php取决于你想用哪种方式展示这个食谱列表:

方案一:用分类法存档模板(推荐,符合WordPress规范)

如果你的食谱是注册了自定义文章类型(比如recipe)和对应的自定义分类法(比如recipe_category),那WordPress自带的分类存档页(比如yourdomain.com/recipe_category/中式料理/)就是天然的分类管理入口。这种情况下,你不需要修改通用的archive.php,而是应该创建针对性的存档模板,避免影响其他文章类型的存档页:

  1. 在你的子主题目录下新建文件archive-recipe_category.php(文件名格式是archive-{分类法slug}.php,记得替换成你实际的分类法slug)
  2. 复制父主题archive.php里的内容到这个新文件,然后修改循环部分,替换成你需要的食谱摘要展示逻辑:
    <?php if ( have_posts() ) : ?>
        <!-- 顶部分类筛选导航 -->
        <div class="recipe-category-filter">
            <?php
            wp_list_categories(array(
                'taxonomy' => 'recipe_category', // 替换成你的分类法slug
                'title_li' => '',
                'show_count' => true,
                'current_category' => get_queried_object_id()
            ));
            ?>
        </div>
    
        <!-- 食谱列表循环 -->
        <div class="recipe-list">
            <?php while ( have_posts() ) : the_post(); ?>
                <article class="recipe-item">
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <!-- 显示所属分类 -->
                    <div class="recipe-categories">
                        <?php
                        $categories = get_the_terms(get_the_ID(), 'recipe_category');
                        if ($categories) {
                            echo '<span>分类:</span>';
                            foreach ($categories as $cat) {
                                echo '<a href="' . get_term_link($cat) . '">' . $cat->name . '</a> ';
                            }
                        }
                        ?>
                    </div>
                    <!-- 显示ACF自定义摘要字段 -->
                    <div class="recipe-summary">
                        <?php echo get_field('recipe_summary'); // 替换成你的ACF字段名 ?>
                    </div>
                </article>
            <?php endwhile; ?>
        </div>
    
        <!-- 分页(如果需要) -->
        <?php the_posts_navigation(); ?>
    <?php else : ?>
        <p>暂无食谱内容</p>
    <?php endif; ?>
    
  3. 保存后,访问你的分类存档页就能看到分类筛选+食谱摘要的效果了,样式可以复用你之前在single.php里写的类名,快速统一风格。

方案二:自定义页面模板(适合指定单独页面展示)

如果你想在某个特定页面(比如yourdomain.com/recipes/)展示所有食谱的分类管理与摘要,那可以创建一个自定义页面模板:

  1. 在子主题目录新建page-recipes.php,开头加上模板标识:
    <?php
    /*
    Template Name: 食谱汇总页
    */
    get_header(); ?>
    
  2. 接着写查询逻辑,用WP_Query获取所有食谱文章,同时添加分类筛选:
    <!-- 分类筛选下拉 -->
    <div class="recipe-filter-dropdown">
        <form method="get" action="<?php echo get_permalink(); ?>">
            <?php
            wp_dropdown_categories(array(
                'taxonomy' => 'recipe_category',
                'name' => 'recipe_cat',
                'show_option_all' => '所有分类',
                'selected' => isset($_GET['recipe_cat']) ? $_GET['recipe_cat'] : 0
            ));
            ?>
            <button type="submit">筛选</button>
        </form>
    </div>
    
    <!-- 构建查询参数 -->
    <?php
    $args = array(
        'post_type' => 'recipe', // 替换成你的自定义文章类型slug
        'posts_per_page' => 12, // 每页显示数量,-1表示全部
        'orderby' => 'title',
        'order' => 'ASC'
    );
    
    // 如果有筛选分类,添加到查询参数
    if (isset($_GET['recipe_cat']) && $_GET['recipe_cat'] != 0) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'recipe_category',
                'field' => 'id',
                'terms' => $_GET['recipe_cat']
            )
        );
    }
    
    $recipe_query = new WP_Query($args);
    ?>
    
    <!-- 食谱列表循环 -->
    <?php if ($recipe_query->have_posts()) : ?>
        <div class="recipe-list">
            <?php while ($recipe_query->have_posts()) : $recipe_query->the_post(); ?>
                <article class="recipe-item">
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <div class="recipe-categories">
                        <?php
                        $categories = get_the_terms(get_the_ID(), 'recipe_category');
                        if ($categories) {
                            echo '<span>分类:</span>';
                            foreach ($categories as $cat) {
                                echo '<a href="' . get_term_link($cat) . '">' . $cat->name . '</a> ';
                            }
                        }
                        ?>
                    </div>
                    <div class="recipe-summary">
                        <?php echo get_field('recipe_summary'); ?>
                    </div>
                </article>
            <?php endwhile; ?>
        </div>
        <?php wp_reset_postdata(); // 重置查询 ?>
    <?php else : ?>
        <p>暂无食谱内容</p>
    <?php endif; ?>
    
    <?php get_footer(); ?>
    
  3. 后台新建页面,选择“食谱汇总页”模板,发布后访问该页面即可看到效果。

小提示

  • 所有修改都要在子主题里操作,避免主题更新丢失代码
  • 替换代码里的recipe(自定义文章类型slug)、recipe_category(分类法slug)、recipe_summary(ACF字段名)为你实际使用的名称
  • 分类筛选的样式可以根据你的网站设计调整,比如改成标签式链接或者侧边栏菜单

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 10:16:34