Custom post type clean url

时间:2015-05-24 作者:ronnyrr

我正在尝试为我的自定义帖子类型创建干净的url,但我似乎不知道如何才能做到这一点。

首先,我使用publicly_queryablequery_var 在我的公共职能中register_post_type: http://codex.wordpress.org/Function_Reference/register_post_type

我还发现了这个stackexchange主题,我添加了一些类,但无法正常工作:Clean URL permalink for custom post type

我正在使用以下代码注册我的自定义帖子类型:

register_post_type(\'team\' , array(
  \'labels\' => $labels,
  \'public\' => true,
  \'has_archive\' => false,
  \'menu_position\' => 5,
  \'menu_icon\' => get_stylesheet_directory_uri(). \'/teamProfiles/team-icon.png\',
  \'supports\' => array(\'title\', \'editor\', \'thumbnail\'),
  \'rewrite\' => false
));
如上所述,我尝试从法典中添加一些值,如下所示:

\'rewrite\' => array(\'slug\' => \'team\'),
\'query_var\' => \'team\',
\'publicly_queryable\' => true
可能需要注意的是,我已经将我的自定义帖子类型构建为一个PHP类。希望有人能帮我。

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

Generate WP 可以帮助您生成干净的CPT。如果这不起作用,请尝试该网站以确保没有遗漏任何内容。

<小时>

// Register Custom Post Type
function custom_post_type() {

    $labels = array(
        \'name\'                  => _x( \'Teams\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'         => _x( \'Team\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'             => __( \'Team\', \'text_domain\' ),
        \'name_admin_bar\'        => __( \'Team\', \'text_domain\' ),
        \'parent_item_colon\'     => __( \'Parent Team:\', \'text_domain\' ),
        \'all_items\'             => __( \'All Teams\', \'text_domain\' ),
        \'add_new_item\'          => __( \'Add New Team\', \'text_domain\' ),
        \'add_new\'               => __( \'Add New\', \'text_domain\' ),
        \'new_item\'              => __( \'New Team\', \'text_domain\' ),
        \'edit_item\'             => __( \'Edit Team\', \'text_domain\' ),
        \'update_item\'           => __( \'Update Team\', \'text_domain\' ),
        \'view_item\'             => __( \'View Team\', \'text_domain\' ),
        \'search_items\'          => __( \'Search Team\', \'text_domain\' ),
        \'not_found\'             => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
        \'items_list\'            => __( \'Teams list\', \'text_domain\' ),
        \'items_list_navigation\' => __( \'Teams list navigation\', \'text_domain\' ),
        \'filter_items_list\'     => __( \'Filter teams list\', \'text_domain\' ),
    );
    $args = array(
        \'label\'                 => __( \'Team\', \'text_domain\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'thumbnail\', ),
        \'taxonomies\'            => array( \'category\', \'post_tag\' ),
        \'hierarchical\'          => true,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'menu_icon\'             => get_stylesheet_directory_uri(). \'/teamProfiles/team-icon.png\',
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => false,       
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\'       => \'page\',
    );
    register_post_type( \'team\', $args );

}
add_action( \'init\', \'custom_post_type\', 0 

结束

相关推荐

Theme URLs problem

我编写了一篇主题文章(编辑:可能是single.php 模板)使用该函数get_userdata( $_GET[\'p\'] ). 但主管理员将URL重新格式化为:~/author/gamelaster (默认值为?author=1) 现在,这是不正确的工作。如何获取此URL格式的作者id?或如何从数据库中获取用户slug+get id?但是,我不能使用标准的author函数/post函数(get\\u ID等)。