我有一个名为 destination 的自定义帖子类型和名为 regions 的自定义分类法(使用亚洲,欧洲,非洲等术语)我创建了一个目的地( venice )并将其与一个区域( europe )相关联 . 现在我已经尝试了几个小时让CPT的永久链接像 /%region%/%destination% (例如http://example.com/europe/venice)一样工作,但它一直给我 404 Not found . 这是相关代码:

add_action("init", "register_region_taxonomy");
add_action("init", "create_destinations_post_type");
add_filter("post_type_link", "region_permalink", 10, 3);

这就是我创建自定义分类法的方法:

function register_region_taxonomy() {
    $labels = array(
        "name" => _x("Destination Region", "taxonomy general name"),
        "singular_name" => _x("Region", "taxonomy singular name"),
        "search_items" => __("Search Regions"),
        "popular_items" => __("Popular Regions"),
        "all_items" => __("All Regions"),
        "parent_item" => null,
        "parent_item_colon" => null,
        "edit_item" => __("Edit Region"),
        "update_item" => __("Update Region"),
        "add_new_item" => __("Add New Region"),
        "new_item_name" => __("New Region"),
        "separate_items_with_commas" => __("Separate regions with commas"),
        "add_or_remove_items" => __("Add or remove regions"),
        "choose_from_most_used" => __("Choose from the most used regions"),
        "menu_name" => __("Regions"),
    );

    register_taxonomy("regions", "destination", array(
        "hierarchical" => false,
        "labels" => $labels,
        "show_ui" => true,
        "show_admin_column" => true,
        "update_count_callback" => "_update_post_term_count",
        "query_var" => "region",
        "rewrite" => array("slug" => "region"),
    ));
}

这是我的自定义帖子类型:

function create_destinations_post_type() {

    register_post_type("destination",
        // CPT Options
        array(
            "labels" => array(
                "name" => __("destinations"),
                "singular_name" => __("destination")
            ),
            "public" => true,
            "publicly_queryable" => true,
            "has_archive" => true,
            "show_ui" => false,
                "not_found" => "No destinations Found",
            "rewrite" => array("slug" => "%region%", "with_front" => false)
            )
    );
}

这就是我希望它改变目的地的固定链接的方式:

function region_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, "%region%") === FALSE)
        return $permalink;

    // Get post
    $post = get_post($post_id);
    if (!$post)
        return $permalink;

    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, "regions");

    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
        $taxonomy_slug = $terms[0]->slug;
    else
        $taxonomy_slug = "no-region";

    return str_replace("%region%", $taxonomy_slug, $permalink);
}

我一步一步跟着_1676342,但无济于事 . 我尝试进入永久链接设置页面并再次保存 - 如上所述here - 但无效(我的永久链接设置设置为 Post name ) . 我在创建CPT后尝试调用 flush_rewrite_rules() 但是不行,它只是不起作用 . 我不确定我是否能够正确使用this SO post,因为我改变了这样的解决方案:

add_filter("rewrite_rules_array", "add_destination_rewrite_rules");

function add_destination_rewrite_rules($rules) {
    $new = array();
    $new['([^/]+)/(.+)/?$'] = 'index.php?destination=$matches[1]';
    $new['(.+)/?$'] = 'index.php?region=$matches[0]';

    return array_merge($new, $rules); // Ensure our rules come first
}

没有成功,所以我现在已经恢复了这一改变 .

如果我尝试http://example.com/venice它确实进入永久链接钩子并且我的浏览器上的URL更改为http://example.com/europe/venice但最终它向我显示了邪恶的404页面 . 我在这里做错了什么线索?我的.htaccess看起来像这样:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wpsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wpsite/index.php [L]
</IfModule>

# END WordPress