带有自定义帖子类型的不需要的URL

时间:2017-05-19 作者:Abuzer Killibacak

我创建了一个自定义帖子类型。这是一本书,我的url就像一个例子。com/book/a-book我展示了有关示例的书籍。com/books,但有一个例子。com/book如何删除该URL或显示404。在本例中,我的站点地图显示url:example。com/书本

function book_post_type() {
    $labels = array(
        \'name\'                => _x( \'Books\', \'Post Type General Name\', \'sv\' ),
        \'singular_name\'       => _x( \'Book\', \'Post Type Singular Name\', \'sv\' ),
        \'menu_name\'           => __( \'Books\', \'sv\' ),
        \'parent_item_colon\'   => __( \'Parent Book\', \'sv\' ),
        \'all_items\'           => __( \'All Books\', \'sv\' ),
        \'view_item\'           => __( \'View Book\', \'sv\' ),
        \'add_new_item\'        => __( \'Add New Book\', \'sv\' ),
        \'add_new\'             => __( \'Add New\', \'sv\' ),
        \'edit_item\'           => __( \'Edit Book\', \'sv\' ),
        \'update_item\'         => __( \'Update Book\', \'sv\' ),
        \'search_items\'        => __( \'Search Books\', \'sv\' ),
        \'not_found\'           => __( \'Not Found\', \'sv\' ),
        \'not_found_in_trash\'  => __( \'Not Found In Trash\', \'sv\' ),
    );
    $args = array(
        \'label\'               => __( \'books\', \'sv\' ),
        \'description\'         => __( \'Books\', \'sv\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'thumbnail\', \'editor\', \'revisions\', ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => false,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => false,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );
    register_post_type( \'book\', $args );
}
add_action( \'init\', \'book_post_type\', 0 );

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

function rt_custom_post() {
  register_post_type( \'book\',
    array(
    \'labels\' => array(
        \'name\' => __( \'Books\' ),
        \'singular_name\' => __( \'Book\' ), 
      ),
    \'public\' => true,
    \'has_archive\' => false, //this will eliminate the archive page
    \'supports\' => array(\'title\',\'editor\',\'thumbnail\'),
    \'publicly_queryable\'  => true
    )
  );
如果您添加:

\'has_archive\' => false,
对于您的注册帖子类型,它将删除存档页。请记住,在进行更改后,通过访问设置->永久链接页面刷新永久链接。

确保没有创建名为book的页面。

结束

相关推荐

Rewriting WordPress URLs

我有一个插件,它通过一个短代码通过GET请求获取输入。快捷码放在带有地址/搜索的页面上,用于执行此特定搜索的查询是/搜索/?VenuName=。这很好用。但是,我需要重写它,以便当用户转到/地点/时,它加载上述搜索。This gist 包含我的原始代码(失败),以及Pippin关于我应该在哪里的建议(也失败)。我已经尝试了我能想到的一切,它总是导致404错误,或者重定向到主页(这毫无意义)。我还尝试了通过Rewrite plugin, 结果是一样的。如果您能提供任何帮助,我们将不胜感激。不幸的是,我在这个