带有HAS_ARCHIVE的CUSTOM_POST_TYPE不会更改固定链接

时间:2016-03-01 作者:renathy

我无法重写自定义帖子类型permalink。

 $labels = array(
            \'name\' => __(\'VmGallery\', THEME_TEXT_DOMAIN, THEME_TEXT_DOMAIN),
            \'singular_name\' => __(\'VmGallery\', THEME_TEXT_DOMAIN),
            \'add_new\' => __(\'Add New\', THEME_TEXT_DOMAIN),
            \'add_new_item\' => __(\'Add New Gallery\', THEME_TEXT_DOMAIN),
            \'edit_item\' => __(\'Edit Gallery\', THEME_TEXT_DOMAIN),
            \'new_item\' => __(\'New Gallery\', THEME_TEXT_DOMAIN),
            \'all_items\' => __(\'All Galleries\', THEME_TEXT_DOMAIN),
            \'view_item\' => __(\'View Gallery\', THEME_TEXT_DOMAIN),
            \'search_items\' => __(\'Search Gallery\', THEME_TEXT_DOMAIN),
            \'not_found\' => __(\'No galleries found\', THEME_TEXT_DOMAIN),
            \'not_found_in_trash\' => __(\'No galleries found in the Trash\', THEME_TEXT_DOMAIN),
            \'parent_item_colon\' => \'\',
            \'menu_name\' => __(\'Gallery\', THEME_TEXT_DOMAIN),
        );
$supports = array(\'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\', \'post-formats\', \'author\', \'excerpt\');

 $args = array(
            \'labels\' => $labels,
            \'description\' => \'Galleries specific information\',
            \'public\' => true,
            \'menu_position\' => 6,
            \'menu_icon\' => \'dashicons-format-image\',
            \'supports\' => $supports,
            \'taxonomies\' => array(\'gallery_categories\', \'post_tag\'),
            \'has_archive\' => \'ggg\'
            /*\'rewrite\' => array(
                \'slug\' => $slug,
                \'with_front\' => false
            ),*/
        );

        register_post_type(\'vmgallery\', $args);
我有php文件:archive-vmgallery.php 我认为应该在URL为myurl.com/ggg. 但它显示404错误页面。如果我使用post类型而不是“ggg”:myurl。com/vmgallery,然后archive-vmgallery.php 已加载。为什么?我想‘has\\u archive’=>‘ggg’允许重写slug?

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

每次更改重写规则时,都需要在数据库中重建重写规则。你能做到的flush_rewrite_rules() 函数,但不要在每次页面加载中都使用它,您只需要执行一次。一般来说,一个很好的钩子就是register_actionvation_hook() 插件的。当您的插件被停用时,您还应该重建重写规则,以便删除自定义重写规则:

register_activation_hook( __FILE__, function () {
    cyb_register_vmgallery_post_type();
    flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
    flush_rewrite_rules();
} );
add_action( \'init\', \'cyb_register_vmgallery_post_type\' );
function cyb_register_vmgallery_post_type() {
   $labels = array(
            \'name\' => __(\'VmGallery\', THEME_TEXT_DOMAIN),
            \'singular_name\' => __(\'VmGallery\', THEME_TEXT_DOMAIN),
            \'add_new\' => __(\'Add New\', THEME_TEXT_DOMAIN),
            \'add_new_item\' => __(\'Add New Gallery\', THEME_TEXT_DOMAIN),
            \'edit_item\' => __(\'Edit Gallery\', THEME_TEXT_DOMAIN),
            \'new_item\' => __(\'New Gallery\', THEME_TEXT_DOMAIN),
            \'all_items\' => __(\'All Galleries\', THEME_TEXT_DOMAIN),
            \'view_item\' => __(\'View Gallery\', THEME_TEXT_DOMAIN),
            \'search_items\' => __(\'Search Gallery\', THEME_TEXT_DOMAIN),
            \'not_found\' => __(\'No galleries found\', THEME_TEXT_DOMAIN),
            \'not_found_in_trash\' => __(\'No galleries found in the Trash\', THEME_TEXT_DOMAIN),
            \'parent_item_colon\' => \'\',
            \'menu_name\' => __(\'Gallery\', THEME_TEXT_DOMAIN),
        );
    $supports = array(\'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\', \'post-formats\', \'author\', \'excerpt\');

    $args = array(
            \'labels\' => $labels,
            \'description\' => \'Galleries specific information\',
            \'public\' => true,
            \'menu_position\' => 6,
            \'menu_icon\' => \'dashicons-format-image\',
            \'supports\' => $supports,
            \'taxonomies\' => array(\'gallery_categories\', \'post_tag\'),
            \'has_archive\' => \'ggg\'
            /*\'rewrite\' => array(
                \'slug\' => $slug,
                \'with_front\' => false
            ),*/
        );

        register_post_type(\'vmgallery\', $args);

}

相关推荐