重写段代码,添加分类法名称为archive.php而不是single.php


Rewrite slug adding taxonomy name call archive.php instead of single.php WordPress

我已经为E.G. Movie添加了自定义帖子类型,并注册了自定义分类法。

我正在通过在post type slug之前添加自定义分类法名称来重写slug。

我在register post type中添加了重写参数:

'rewrite' => array( 'slug' => 'movie/%tax_name%', 'with_front' => false )

及以下分类参数:

array(
    'labels'            => $lables,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'hierarchical'      => true,
    'has_archive'       => false,
    'rewrite'           => array( 'slug' => 'type', 'with_front' => false )
);

我还添加了以下行来初始化操作:

add_rewrite_rule( '^movies/(.*)/(.*)/([^/]+)/?$','index.php?type=$matches[2]', 'top' );

对于'post_type_link'操作:

// Add parent taxonomy name / module name
$terms = wp_get_post_terms( $post->ID, 'type', array() );
$term = isset($terms['0']) ? $terms['0'] : '';
if( !empty($term->slug) ) {
    $link = str_replace( '%tax_name%', $term->slug, $link );
} else {
    $link = str_replace( '/%tax_name%', '', $link );
}

现在我的URL是http://yourdomain.com/movie/fun/movie_name

我从这里得到了一些参考:https://wordpress.stackexchange.com/questions/94817/add-category-base-to-url-in-custom-post-type-taxonomy

但是,问题是,当我查看该帖子时,它称为archive.php而不是single.php,并且还执行归档页面并显示所有页面。

请提供解决方案。任何建议都可以接受。

谢谢

我自我研发后得到了答案,我需要改变:

add_rewrite_rule( '^movies/(.*)/(.*)/([^/]+)/?$','index.php?type=$matches[2]', 'top' );

:

add_rewrite_rule( '^movies/(.*)/([^/]+)/?$','index.php?movies=$matches[2]', 'top' );