多个POST类型-共享相同的重写插件?

时间:2011-05-19 作者:Joe

我遇到了另一个精彩的404问题。我正在尝试使用两种不同的帖子类型,它们共享相同的重写段。我刷新了我的重写规则,当我测试其中一个CPT时,另一个得到404。我希望对两者都使用的重写是:

\'rewrite\' => array(\'slug\' => \'team/%teamtype%\'),
有人知道怎么处理吗?

add_action( \'init\', \'create_rider_type\' );

function create_rider_type() {

register_post_type(\'riders\', 
array(  
    \'description\' => \'rider custom post type\',
    \'show_ui\' => true,
    \'menu_position\' => 5,
    \'menu_icon\' => get_stylesheet_directory_uri() . \'/images/riders.png\',
    \'exclude_from_search\' => false,
    \'labels\' => array(
        \'name\' => \'Team Riders\',
        \'singular_name\' => \'Rider\',
        \'add_new\' => \'Add New rider\',
        \'add_new_item\' => \'Add New rider\',
        \'edit\' => \'Edit riders\',
        \'edit_item\' => \'Edit rider\',
        \'new_item\' => \'New rider\',
        \'view\' => \'View rider\',
        \'view_item\' => \'View rider\',
        \'search_items\' => \'Search riders\',
        \'not_found\' => \'No riders found\',
        \'not_found_in_trash\' => \'No riders found in Trash\',
        \'parent\' => \'Parent rider\',
    ),
    \'hierarchical\' => false,
    \'supports\' => array(\'title\',\'editor\',\'excerpt\', \'trackbacks\',\'custom-fields\', \'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\'),
    \'public\' => true,
    \'rewrite\' => array(\'slug\' => \'team/%teamtype%\'),
    \'taxonomies\' => array(\'teamtype\')
    ) 
);


}





add_action( \'init\', \'create_sponsor_type\' );

function create_sponsor_type() {

register_post_type(\'sponsors\', 
array(  
    \'description\' => \'sponsor custom post type\',
    \'show_ui\' => true,
    \'menu_position\' => 5,
    \'menu_icon\' => get_stylesheet_directory_uri() . \'/images/sponsors.png\',
    \'exclude_from_search\' => false,
    \'labels\' => array(
        \'name\' => \'Team sponsors\',
        \'singular_name\' => \'sponsor\',
        \'add_new\' => \'Add New sponsor\',
        \'add_new_item\' => \'Add New sponsor\',
        \'edit\' => \'Edit sponsors\',
        \'edit_item\' => \'Edit sponsor\',
        \'new_item\' => \'New sponsor\',
        \'view\' => \'View sponsor\',
        \'view_item\' => \'View sponsor\',
        \'search_items\' => \'Search sponsors\',
        \'not_found\' => \'No sponsors found\',
        \'not_found_in_trash\' => \'No sponsors found in Trash\',
        \'parent\' => \'Parent sponsor\',
    ),
    \'hierarchical\' => false,
    \'supports\' => array(\'title\',\'editor\',\'excerpt\', \'trackbacks\',\'custom-fields\', \'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\'),
    \'public\' => true,
    \'rewrite\' => array(\'slug\' => \'team/%teamtype%\'),
    \'taxonomies\' => array(\'teamtype\')
    ) 
);

}
*************更新***********************************

我发布的原始CPT重写代码经过了简化,因此我可以更直截了当地指出这一点,但是如果您能看到我如何使用自定义分类法处理这些永久链接,那么这可能更有意义。我已更新代码以显示。

我真的希望把它们作为单独的职位类型——对于组织,以及每个职位的单独元数据库。请检查CPT的更新重写,以及下面我的分类设置:

add_action( \'init\', \'create_team_taxonomies\' );

function create_team_taxonomies() {
register_taxonomy( 
\'teamtype\', 
array(\'riders\',\'sponsors\'),
array( 
    \'labels\' => array(
        \'name\' => \'Team Types\',
        \'singular_name\' => \'Team Type\',
        \'search_items\' =>  \'Search Team Types\',
        \'popular_items\' => \'Popular Team Types\',
        \'all_items\' => \'All Team Types\',
        \'parent_item\' => \'Parent Team Type\',
        \'parent_item_colon\' => \'Parent Team Type:\',
        \'edit_item\' => \'Edit Team Type\', 
        \'update_item\' => \'Update Team Type\',
        \'add_new_item\' => \'Add New Team Type\',
        \'new_item_name\' => \'New Team Type Name\'
    ), 
\'hierarchical\' => true, 
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => \'teamtype\',
\'show_tagcloud\' => true,
\'rewrite\' => array( \'slug\' => \'team\', \'with_front\' => false) 
) 
);

}
下面是我在选择分类法并发布帖子时如何设置重写:

add_filter(\'post_link\', \'teamtypepermalink\', 10, 3);
add_filter(\'post_type_link\', \'teamtypepermalink\', 10, 3);

function teamtypepermalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%teamtype%\') === FALSE) return $permalink;

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

    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, \'teamtype\');    
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = \'not-specified\';

return str_replace(\'%teamtype%\', $taxonomy_slug, $permalink);
}

5 个回复
SO网友:dotancohen

我刚刚做了一个项目,其中两个自定义帖子类型需要共享同一个slug。The trick is to overwrite the query vars via the request filter. 让我们将post类型称为“foo”和“bar”,它们将共享slug“foo”。

以下代码是粗略的,仅以内存为例。根据口味加盐,不要复制糊状物:

add_filter(\'request\', \'overwriteQueryVars\', 10, 1);

function overwriteQueryVars($query)
{
    if ( empty($query[\'post_type\']) || $query[\'post_type\']!=\'foo\' ) {
        return $query;
    }

    // Run an SQL query that looks like this, against both post types
    $sql = "SELECT ID FROM wp_posts WHERE post_type=\'foo\' and post_name=\'%s\'";
    $post_name = urlencode($query[\'name\']);

    // If you\'ve found that the row exists for post_type \'bar\' but not \'foo\',
    // then simply return the $query array unaltered
    if ( in_foo_but_not_bar ) {
        return $query;
    }

    // If you\'ve found that the row exists for post_type \'bar\' but not \'foo\',
    // then rewrite the $query array:

    // Original:
    $query = array (
        \'page\' => \'\',
        \'foo\' => \'some-nice-post\',
        \'post_type\' => \'foo\',
        \'name\' => \'some-nice-post\',
    );

    // Make it look like this:
    $query = array (
        \'page\' => \'\',
        \'bar\' => \'some-nice-post\',
        \'post_type\' => \'bar\',
        \'name\' => \'some-nice-post\',
    );

}

SO网友:Azer

I know this question is very old, but when I was looking for a solution to a similar problem, this question came closest to what I was trying to achieve, and since it came up in my search I figured there might not yet be any better solutions available. So I\'ll add the solution I came up with below in case it might help someone later on with a similar issue.

我也有类似的问题。我想有一个新的自定义帖子类型,可以与其他帖子类型共享slug。就我而言,我有一个帖子类型叫做country-info 我想使用与标准相同的permalink结构page 岗位类型。

换句话说,我希望它可以使用example.com/page-name/ 而不是example.com/country-info/page-name.

我就是这样解决的:

// I found it easiest to grab the request in the parse_request hook. There you can see
// the requested slug, as well as if the requested page existed or not.

add_action(\'parse_request\', \'overwriteRequest\', 10, 1);
function overwriteRequest($query) {

    // If the request matched an existing page, the query_vars array will contain the
    // post_type, but otherwise it will only contain an error.
    // So we only execute our code if the post_type is empty
    // On the top page, the query_vars is an empty array, so we check for that too!
    if ( count($query->query_vars) > 0 && empty($query->query_vars[\'post_type\'])) {

        // The request contains the string that was requested.
        $pageName = $query->request;
        $postType = \'country-info\';

        // Check if post with the requested slug exists in your custom post type
        $result = get_posts(array(
            \'post_type\' => $postType,
            \'name\' => $pageName
        ));

        // If it doesn\'t, just return the query
        if (count($result) < 1) {

            return $query;

        // If it does, create a new query_vars array to replace the old one.
        } else {
            $new_query = array(
                \'page\' => \'\',
                \'country-info\' => $pageName,
                \'post_type\' => $postType,
                \'name\' => $pageName
            );
            $query->query_vars = $new_query;
            return $query;
        }

    } else {
        return $query;
    }
}

Please note:

在我的例子中,我正在寻找的请求都位于域的根级别,如example.com/page-name, 但是如果您的自定义帖子类型位于如下目录中example.com/some-directory/post-name/, 然后,您需要更改代码中的一项内容:

$pageName = $query->request;

需要是

$pageName = preg_replace(\'/my-directory-name\\//\', \'\', $query->request)

否则,get\\u posts将尝试查找slug包含您的目录的帖子,这将始终为您提供零结果。这个preg\\u replace只是从字符串中删除您的目录名。

记住更换my-directory-name 使用真实的目录名!

此解决方案已在WordPress 4.9.7上测试并运行

SO网友:MZAweb

据我所知,你不能那样做。每个slug都转换为查询特定对象类型(分类法或post类型)的query\\u vars项。如果分析WP\\u查询,您将看到它正在查询第一个帖子类型。

也许这里的WP忍者可以给我们一些启示,但我几乎可以肯定你做不到。或者至少,你不应该。

SO网友:Norcross

由于CPT(以及帖子/页面)是构建的,这是不可能的。slug是为特定类型保留的,理解/假设2个CPT=2种不同类型的内容。

根据您的代码,似乎您在CPT中拥有的每一项都可以更好地用作自定义分类法,两者都连接到一个CPT(我猜是某种事件?)

SO网友:Jan Fabry

您的重写规则将相互覆盖,WordPress将只查询其中一种帖子类型。我认为这是最新的一个被注册的“胜利”,所以/testslug/bmw/ 将始终查询带有slug的帖子bmw 和岗位类型sponsors. 如果你去/testslug/some-name/some-name 是附加信息,它仍将查询帖子类型sponsors 和后段塞some-name. 它将找不到帖子,因此会给你一个404。这发生在WordPress加载模板页面之前为您执行的“默认”查询中,因此您无法在single-sponsors.php 例如,模板文件(您可以在404.php, 但之后会变得丑陋)。

最干净的解决方案是替换进行URL解析的部分逻辑,并使其接受“不明确”的URL。测试它是骑手还是赞助商,并更改参数,以便WordPress将继续使用骑手或赞助商。迈克once provided a way to do this 对于页面和分类法,应该可以将其扩展为仅用于帖子。

结束

相关推荐

curl problem or permalinks

我刚刚配置了我的VPS,我使用的是Centos,一切都很好,但如果我将永久链接设置为自定义结构,然后接受主页,没有帖子出现,它会显示404页,我想这是因为我没有启用curl,但我不知道我的php在哪里。我的centos中的ini文件?好的,我的卷曲被启用了,我检查过了phpinfo(); 这里是URLhttp://74.117.158.182/info.php但如果我在我的wordpress中设置了永久链接,那么接受主页,所有都会给我404页,你可以在这个URL上查看http://mbas.co.in如果