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

WordPress使用Osetin Meal Planer时如何给自定义文章类型URL添加%author%结构?

实现方案

你可以通过WordPress内置钩子修改自定义文章类型重写规则实现需求,无需修改插件核心代码,操作步骤如下:

  • 第一步:添加自定义代码
    将以下代码添加到当前激活主题的functions.php文件,或者专用的自定义代码片段插件中:
// 注册author重写标签
add_action('init', function() {
    add_rewrite_tag('%cpt_author%', '([^/]+)');
});

// 替换自定义文章类型永久链接中的作者占位符
add_filter('post_type_link', function($permalink, $post) {
    // 匹配videos自定义文章类型,如需适配食谱CPT可添加|| $post->post_type === 'recipe'判断
    if ($post->post_type !== 'videos' || strpos($permalink, '%cpt_author%') === false) {
        return $permalink;
    }
    $author = get_user_by('id', $post->post_author);
    return $author ? str_replace('%cpt_author%', $author->user_nicename, $permalink) : $permalink;
}, 10, 2);

// 修改自定义文章类型重写规则
add_filter('register_post_type_args', function($args, $post_type) {
    // 适配videos和食谱CPT
    if (in_array($post_type, ['videos', 'recipe'])) {
        $args['rewrite'] = [
            'slug' => $post_type . '/%cpt_author%',
            'with_front' => false,
            'feeds' => true
        ];
    }
    return $args;
}, 20, 2);
  • 第二步:刷新重写规则
    进入WordPress后台「设置」→「固定链接」页面,直接点击「保存更改」即可完成规则刷新,无需修改原有固定链接设置。

适配OSETIN MEAL PLANER插件说明

该插件创建的食谱自定义文章类型默认slug为recipe,上述代码已经做了兼容处理,如果你需要给食谱类文章也添加相同的作者URL结构,无需额外修改代码即可生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:51:01