【问题标题】:WordPress custom post type rewrite rule same as page poyt typeWordPress 自定义帖子类型重写规则与页面类型相同
【发布时间】:2015-08-06 21:00:37
【问题描述】:

我创建了自定义帖子类型:书籍 并为此创建了重写规则:

$new_rules[ '([^/]+)$' ] = 'index.php?post_type=book&name=$matches[1]';

我工作正常!图书网址是: http://domain.com/bookname/

但是当我打开页面(默认页面 posty 类型)时,我收到 404 错误。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    在注册您的自定义帖子类型时使用 with_front 设置为 false 将从您的自定义帖子类型中删除 slug,

    'rewrite' => array('slug' => 'book', 'with_front' => false),

    示例:使用rewrite 规则注册您的帖子类型时。参见register_post_type手册

    add_action('init', 'codex_book_init');
    
    function codex_book_init()
    {
        $labels = array(
            'name' => _x('Books', 'post type general name', 'your-plugin-textdomain'),
            'singular_name' => _x('Books', 'post type singular name', 'your-plugin-textdomain'),
            'menu_name' => _x('Books', 'admin menu', 'your-plugin-textdomain'),
            'name_admin_bar' => _x('Books', 'add new on admin bar', 'your-plugin-textdomain'),
            'add_new' => _x('Add New', 'book', 'your-plugin-textdomain'),
            'add_new_item' => __('Add New Books', 'your-plugin-textdomain'),
            'new_item' => __('New Books', 'your-plugin-textdomain'),
            'edit_item' => __('Edit Books', 'your-plugin-textdomain'),
            'view_item' => __('View Books', 'your-plugin-textdomain'),
            'all_items' => __('All Books', 'your-plugin-textdomain'),
            'search_items' => __('Search Books', 'your-plugin-textdomain'),
            'parent_item_colon' => __('Parent Books:', 'your-plugin-textdomain'),
            'not_found' => __('No books found.', 'your-plugin-textdomain'),
            'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain')
        );
    
        $args = array(
            'labels' => $labels,
            'description' => __('Description.', 'your-plugin-textdomain'),
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'book', 'with_front' => false),
            'capability_type' => 'post',
            'has_archive' => true,
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments')
        );
    
        register_post_type('book', $args);
    }
    

    【讨论】:

    • 'with_front' => false 添加,但 URL 仍然:domain.com/book/bookpost/ 不是 domain.com/bookpost/
    • 这是因为您已经注册了这个帖子类型复制我提供的源代码并将自定义帖子类型书更改为另一个名称以进行测试,您将看到 url domain.com/another-name- post/ 我已经用USE Cases 测试了上面的代码,我在已经注册的帖子上遇到了同样的问题,但是使用with_front=>false 用新的测试帖子类型修复了。
    【解决方案2】:
    1. WordPress 使用第一个匹配的规则。
    2. 您的新规则与页面规则相同。
    3. 您的新规则比页面规则更早匹配。
    4. 当然,没有找到与该页名相同的书。
    5. WordPress 在这里停止(参见 1.)并向您显示 404 错误,这实际上是 100% 正确的。

    要解决这个问题,请在您的 register_post_type 调用中启用带有 'with_front' => true 参数的 url base,这样您的 url 将看起来像 domain.com/book/book_name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多