【问题标题】:Rewrite slug adding taxonomy name call archive.php instead of single.php WordPress重写 slug 添加分类名称调用 archive.php 而不是 single.php WordPress
【发布时间】:2017-02-27 17:02:58
【问题描述】:

我为 E.G. 添加了自定义帖子类型。电影并注册自定义分类。

我正在通过在帖子类型 slug 之前添加自定义分类名称来重写 slug。

我在注册帖子类型中添加了重写参数:

'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 );
}

现在我的网址是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,并且还像存档页面一样执行并显示所有页面。

请提供一些解决方案。接受任何建议。

谢谢

【问题讨论】:

  • 我已经打印了每个链接的永久链接,它提供了完美的永久链接,例如:http://yourdomain.com/movie/fun/moviename,但是当我访问它时,它会打开存档页面。

标签: php wordpress url-rewriting slug


【解决方案1】:

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

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

收件人:

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

【讨论】:

    猜你喜欢
    • 2018-05-21
    • 2016-08-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多