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

子主题添加Permalink改写代码后出现语法错误,寻求解决办法

Fixing Syntax Error When Updating CPT Slug from "team-member" to "author"

Hey there, let's work through that syntax error you ran into when trying to change your custom post type (CPT) slug from team-member to author.

First, the error you're seeing is almost always tied to either duplicate PHP opening tags (<?php) in your functions.php file, or leftover formatting characters from copying the code incorrectly. Here's how to fix it step by step:

Step 1: Use Clean, Valid PHP Code

The code from your theme provider is correct in theory, but sometimes copying from formatted code blocks can introduce hidden characters. Replace the problematic code with this clean version:

add_filter( 'pgscore_register_cpt_teams', 'pgscore_extend_cpt_teams' );

function pgscore_extend_cpt_teams( $args ){
    // Grab the current rewrite rules for the CPT
    $rewrite_new = $args['rewrite'];
    // Update the slug to "author"
    $rewrite_new['slug'] = 'author';
    // Save the updated rewrite rules back to the CPT args
    $args['rewrite'] = $rewrite_new;
    // Return the modified arguments
    return $args;
}

Critical Note:

If your child theme's functions.php file already starts with <?php at the top, do not add another <?php tag at the start of this code. Duplicate opening tags are a common cause of syntax errors.

Step 2: Verify Your functions.php File Structure

Double-check the rest of your functions.php to make sure there are no unclosed brackets (}) or missing semicolons (;) from previous code. A single missing character earlier in the file can break everything that comes after it.

Step 3: Flush WordPress Rewrite Rules

After saving the corrected code to your child theme's functions.php:

  1. Log into your WordPress admin dashboard
  2. Go to Settings → Permalinks
  3. Don't change any settings—just click the Save Changes button. This forces WordPress to refresh its rewrite rules so the new author slug takes effect.

Bonus: Avoid Slug Conflicts

Keep in mind that author is a default WordPress slug used for author archives. If you run into 404 errors after making this change, you can adjust the rewrite rules to avoid conflicts by adding one line to the function:

add_filter( 'pgscore_register_cpt_teams', 'pgscore_extend_cpt_teams' );

function pgscore_extend_cpt_teams( $args ){
    $rewrite_new = $args['rewrite'];
    $rewrite_new['slug'] = 'author';
    // Prevent conflict with default author archives
    $rewrite_new['with_front'] = false;
    $args['rewrite'] = $rewrite_new;
    return $args;
}

Don't forget to flush the rewrite rules again after making this adjustment!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:56:25