自定义帖子类型和固定链接设置

时间:2016-09-04 作者:jppower175

在网站的根目录中添加Wordpress。希望以页面的URL结构为例。com/{page slug},并以帖子为例。com/academy/{post-slug}。我将永久链接设置为自定义,结构为/academy/%postname%。

这很有效。但是,我有一个自定义的post类型,称为definitions。我想以定义URL结构为例。com/glossary/{定义slug}。目前与上述设置,我得到了一个例子的URL。com/academy/definition/{definition slug}

有没有办法从post permalink设置中排除自定义帖子类型?

以下是我的自定义帖子定义:

function create_posttypes() {
    $labels1 = array(
            \'name\' => __( \'Definitions\' ),
            \'singular_name\' => __( \'Definition\' ),
            \'all_items\' => __( \'All Definitions\' ),
            \'view_item\' => __( \'View Definition\' ),
            \'add_new_item\' => __( \'Add New Definition\' ),
            \'edit_item\' => __( \'Edit Definition\' ),
            \'update_item\' => __( \'Update Definition\' ),
            \'search_items\' => __( \'Search Definitions\' ),
            \'not_found\' => __( \'Not Found\' ),
            \'not_found_in_trash\' => __( \'Not Found in Trash\' ),
        );
    $args1 = array(
            \'label\' => \'Definitions\',
            \'description\' => \'Definitions received\',
            \'labels\' => $labels1,
            \'public\' => true,
            \'has_archive\' => true,
            \'rewrite\' => array(\'slug\', \'definitions\'),
            \'hierarchical\' => false,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'show_in_nav_menus\' => true,
            \'show_in_admin_bar\' => true,
            \'menu_position\' => 5,
            \'can_export\' => true,
            \'exclude_from_search\' => false,
            \'publicly_queryable\' => true,
            \'capability_type\' => \'page\',
            \'supports\' => array(\'title\',\'thumbnail\', \'revisions\', \'page-attributes\')
        );
    register_post_type( \'definitions\', $args1 );
}

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

您可以使用rewrite 参数以禁用URL的前部(/academy/ 在您的情况下),用于自定义帖子类型。

function create_posttypes() {
    $labels1 = array(
            \'name\' => __( \'Definitions\' ),
            \'singular_name\' => __( \'Definition\' ),
            \'all_items\' => __( \'All Definitions\' ),
            \'view_item\' => __( \'View Definition\' ),
            \'add_new_item\' => __( \'Add New Definition\' ),
            \'edit_item\' => __( \'Edit Definition\' ),
            \'update_item\' => __( \'Update Definition\' ),
            \'search_items\' => __( \'Search Definitions\' ),
            \'not_found\' => __( \'Not Found\' ),
            \'not_found_in_trash\' => __( \'Not Found in Trash\' ),
        );
    $args1 = array(
            \'label\' => \'Definitions\',
            \'description\' => \'Definitions received\',
            \'labels\' => $labels1,
            \'public\' => true,
            \'has_archive\' => true,
            \'rewrite\' => array(
                \'slug\' => \'glossary\',
                \'with_front\' => false,
             ),
            \'hierarchical\' => false,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'show_in_nav_menus\' => true,
            \'show_in_admin_bar\' => true,
            \'menu_position\' => 5,
            \'can_export\' => true,
            \'exclude_from_search\' => false,
            \'publicly_queryable\' => true,
            \'capability_type\' => \'page\',
            \'supports\' => array(\'title\',\'thumbnail\', \'revisions\', \'page-attributes\')
        );
    register_post_type( \'definitions\', $args1 );
}

相关推荐

WP JSON list all permalinks

Scenario我正在考虑编写一个自定义的wp json端点,以列出我的wordpress中每个帖子的所有永久链接。Question是否可以使用rest查询+筛选器来完成此操作?例如:。http://wpsite.com/wp-json/wp/v2/posts?filter[only-permalinks]当前的解决方案最后我编写了一个自定义端点,代码如下:添加到函数的底部。phpfunction get_all_slugs() { $posts = get_posts( array(&#