自定义帖子类型帖子,与顶部添加_重写_规则后的帖子冲突

时间:2022-01-19 作者:Patrick Zszywacz

我有这样分类的cpt:

add_action(\'init\', \'zsz_add_posttype\');
function zsz_add_posttype()
{
// -- Taxonomy CATEGORIES  -  [category-a] [category-b] [category-c] [...]
    register_taxonomy(\'success-category\', array(\'our-success\'), array(
        \'hierarchical\' => true,
        \'labels\' => array(\'name\' => __(\'Success Category\', ZSZ_DOMAIN) ),
        \'show_ui\' => true,
        \'show_admin_column\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'success-category\'),
        \'show_in_rest\' => true,
    ));
    // -- OUR SUCCESS
    register_post_type(
        \'our-success\',
        array(
            \'labels\' => array(
                \'name\' => __(\'Our Success\', ZSZ_DOMAIN),
                \'singular_name\' => __(\'Our Success\', ZSZ_DOMAIN)
            ),
            \'public\' => true,
            \'has_archive\' => false,
            \'publicly_queryable\' => true,
            \'exclude_from_search\' => true,
            \'show_in_rest\' => true,
            \'menu_position\' => 5,
            //\'rewrite\' => array(\'slug\' => \'\'),
            \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'excerpt\', \'page-attributes\'),
            \'menu_icon\' => \'dashicons-awards\',
            \'rewrite\' => array(\'slug\' => __(\'our-success\', ZSZ_DOMAIN), \'with_front\' => false),
        )
    );
}
当我使用here:

function wpa_show_permalinks( $post_link, $post ){
    if(!is_object($post) || $post->post_type !== \'our-success\' ){
        return $post_link;
    }

    $terms = wp_get_object_terms( $post->ID, \'success-category\' );
    if( isset($terms[0]->slug) && !empty($terms[0]->slug) ){
        return str_replace( \'our-success\' , $terms[0]->slug , $post_link );
    }
    return $post_link;
}
add_filter( \'post_type_link\', \'wpa_show_permalinks\', 1, 2 );
并添加重写规则:

function wpd_foo_rewrite_rule() {
    add_rewrite_rule(
        \'([^/]+)/([^/]+)(?:/([0-9]+))?/?$\',
        \'index.php?success-category=$matches[1]&our-success=$matches[2]&post_type=our-success\',
        \'top\'
    );
}
add_action( \'init\', \'wpd_foo_rewrite_rule\',1,1 );
自定义类型的单发工作正常,但普通单发帖子给出404。当我更改为添加\\u rewrite\\u规则([…],\'下图)情况正好相反,当单个帖子工作正常时,cpt single给出404。我认为两个重写规则看起来是一样的,Wordpress不承认帖子是定制的。我怎样才能修复它?我做错了什么?

我想要实现的目标:

工作cpt单岗位:example.com/success-category-abc/our-success-postname/example.com/category-name/postname/example.com/%category%/%postname%/

1 个回复
最合适的回答,由SO网友:Patrick Zszywacz 整理而成

我想我解决了我的问题。因此,如果您将WordPress post permalink设置为%category%/%postname%/, 如果你想要自定义帖子类型和分类法,你可以为所有生成的分类法术语(cat-a、cat-b等)准备重写链接。代码:

add_filter( \'rewrite_rules_array\', function($rules) {
    $categories = get_terms([\'taxonomy\' => \'success-category\' ]);
    foreach ($categories as $category){
        $rules = array($category->slug.\'/([^/]*)/?$\' => \'index.php?success-category=\'.$category->slug.\'&our-success=$matches[1]&post_type=our-success\') + $rules;
    }
    return $rules;
});
我也在尝试wpd_foo_rewrite_rule() 函数,但似乎没有“init”操作的术语。所以我删除了function wpd_foo_rewrite_rule()

我离开了function wpa_show_permalinks 未更改。

新创建的术语只有一个问题,但如果刷新\\u rewrite\\u rules()一次,问题就会消失。

<小时/>

Update

我以另外两个函数结束。

首先检查术语是否没有任何关系,并设置将来的标志flush_rewrite_rules() 具有update_option( \'success-category-flush-rewrite-rules\', true );

add_action(\'added_term_relationship\', \'flush_if_term_first_time_in_use\', 10, 3);
function flush_if_term_first_time_in_use( int $object_id, int $tt_id, string $taxonomy ) {
    if( $taxonomy !== \'success-category\' ){
        return;
    }
    if( get_term($tt_id)->count === 0 ){
        update_option( \'success-category-flush-rewrite-rules\', true );
    }
}
第二个仅用于执行flush_rewrite_rules()

function flush_rewrite_after_term_relation() {
    if( get_option(\'success-category-flush-rewrite-rules\') == true){
        flush_rewrite_rules();
        update_option( \'success-category-flush-rewrite-rules\', false );
    }
}
add_action( \'init\', \'flush_rewrite_after_term_relation\',1,1 );

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?