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

如何修改ACF调用的Vegetable自定义文章类型单页链接至single-recipes.php

问题描述

我通过ACF Post Field创建了一个页面,用于展示自定义文章类型Vegetable的指定文章。目前这些文章的超链接指向Vegetable的单页面模板single-vegetable.php,链接示例:

https://www.example.com/vegetable/broccoli

但我希望将链接改为指向single-recipes.php模板,期望的链接格式:

https://www.example.com/recipes/broccoli

当前使用的ACF代码如下:

$vegetables = get_field('vegetables');
if( $vegetables ): 
    foreach( $vegetables as $vegetable ): 
    ?>
        <div class="recipe">
            <a href="<?php the_permalink(); ?>"> // This is the link I need to update
                <div class="recipe-image">
                    <?php
                    $featured_image = has_post_thumbnail() ? get_the_post_thumbnail() : '<img src="' . get_template_directory_uri() . '/images/coming-soon.png" alt="Default Image">';
                    echo $featured_image;
                    ?>
                </div>
                <h3 class="recipe-title">
                    <?php the_title(); ?>
                </h3>
            </a>
        </div>
    <?php endforeach; ?>
    <?php 
    wp_reset_postdata(); ?>
<?php endif; ?>
解决方案

要实现需求,需要完成两个核心步骤:修改链接输出、确保single-recipes.php能正确处理请求。

1. 替换ACF循环中的链接输出

把原来的the_permalink()替换为自定义拼接的URL,直接使用文章别名生成/recipes/xxx格式的链接:

$vegetables = get_field('vegetables');
if( $vegetables ): 
    foreach( $vegetables as $vegetable ): 
        // 直接从$vegetable对象获取文章数据,避免全局数据污染
        $veg_slug = get_post_field('post_name', $vegetable);
        $veg_title = get_post_field('post_title', $vegetable);
        $veg_thumbnail = get_the_post_thumbnail($vegetable) ?: '<img src="' . get_template_directory_uri() . '/images/coming-soon.png" alt="Default Image">';
    ?>
        <div class="recipe">
            <a href="<?php echo home_url('/recipes/' . $veg_slug); ?>">
                <div class="recipe-image">
                    <?php echo $veg_thumbnail; ?>
                </div>
                <h3 class="recipe-title">
                    <?php echo $veg_title; ?>
                </h3>
            </a>
        </div>
    <?php endforeach; ?>
    <?php 
    wp_reset_postdata(); ?>
<?php endif; ?>

2. 添加重写规则与模板映射

现在需要让WordPress识别/recipes/xxx的URL,并加载single-recipes.php模板,同时正确获取对应的Vegetable文章。在主题的functions.php中添加以下代码:

注册重写规则

add_action('init', 'register_vegetable_recipes_rewrite');
function register_vegetable_recipes_rewrite() {
    // 匹配/recipes/xxx格式的URL,将xxx作为Vegetable文章的别名传递
    add_rewrite_rule(
        '^recipes/([^/]+)/?$',
        'index.php?vegetable=$matches[1]',
        'top'
    );
}

添加后需要刷新WordPress固定链接设置(进入后台「设置-固定链接」,直接点击保存即可,无需修改现有配置),让新规则生效。

指定模板加载

添加template_include钩子,让Vegetable文章在通过/recipes/xxx访问时加载single-recipes.php:

add_filter('template_include', 'load_recipes_template_for_vegetable');
function load_recipes_template_for_vegetable($template) {
    global $wp_query;
    // 判断当前请求的是Vegetable文章,且访问路径包含/recipes/
    if (isset($wp_query->query_vars['vegetable']) && strpos($_SERVER['REQUEST_URI'], '/recipes/') !== false) {
        $custom_template = get_stylesheet_directory() . '/single-recipes.php';
        if (file_exists($custom_template)) {
            return $custom_template;
        }
    }
    return $template;
}

完成以上修改后,点击页面中的链接会跳转到/recipes/broccoli格式的地址,同时WordPress会使用single-recipes.php模板渲染对应的Vegetable文章内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:57:51