首页 > 解决方案 > 自定义帖子类型子帖子中子网址的 WordPress 自定义 JS 路由

问题描述

我添加了一个 JS 路由器 ( Navigo.js ) 并喜欢在 Costum Post Type 帖子上使用它。但我无法让它工作。

我喜欢上班的url struktur是这样的:
/[CTP]/[post_title]/[JS router]

function paper_post_type() {
    register_post_type('paper',
        [
            'labels' => array (
                'name' => 'Paper',
                'menu_name' => 'Paper',
            ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'capability_type' => 'post',
            'hierarchical' => true,
            'rewrite' => true,
            'has_archive' => false
        ]
    );
}
add_action('init', 'paper_post_type');

add_action('init', function() {
    add_rewrite_rule( 
        '^paper/([^/]+)/?$',
        'index.php?paper=$matches[1]',
        'top'
    );
});

标签: javascriptwordpress

解决方案


这有效:

add_action('init', function() {
    add_rewrite_rule('^paper/([^/]*)/', 'index.php?paper=$matches[1]', 'top');
});

推荐阅读