wordpress插件中的自定义post-type错误

时间:2016-12-18 02:01:10

标签: php wordpress plugins http-status-code-404 permalinks

我正在为wordpress创建一个插件,我有一个自定义的后期类型,当我将这个插件安装到一个新网站,我在这个新的帖子类型中创建一个帖子我有404错误。如果我去设置 - >永久链接 - >然后单击“保存”。问题是修复。但是,我不想这样做mannualy我想我的插件为我这样做。所以我在互联网上看,每个人都说我必须使用重写功能。但它不适合我...这是我的代码。

<?php

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

// Creating the post type

function eleva_sponsors_custom_post_type() {

    $singular_sponsors = "Sponsor";

    $plural_sponsors = "Sponsors";

    $labels_sponsors = array(

            "name"                  =>        $plural_sponsors,
            "singular_name"         =>        $singular_sponsors,
            "add_new"               =>        "Add New",
            "add_new_item"          =>        "Add New " . $singular_sponsors,
            "edit_item"             =>        "Edit " . $singular_sponsors,
            "new_item"              =>        "New " . $singular_sponsors,
            "view_item"             =>        "View " . $singular_sponsors,
            "search_items"          =>        "Search " . $plural_sponsors,
            "not_found"             =>        "No " . $plural_sponsors." found",
            "not_found_in_trash"    =>        "No " .$plural_sponsors." found in trash",
            "all_items"             =>         "All sponsors"
    );

    $args_sponsors = array(

            "labels"                =>          $labels_sponsors,
            "public"                =>          true,
            "publicity queryable"   =>          true,
            "show_in_nav_menus"     =>          true,
            "show_ui"               =>          true,
            "show_in_menu"          =>          true,
            "show_in_admin_bar"     =>          true,
            "menu_position"         =>          10,
            "menu_icon"             =>          "dashicons-format-gallery",
            "can_export"            =>          true,
            "delete_with_user"      =>          false,
            "hierarchical"          =>          false,
            "has_archive"           =>          true,
            "query_bar"             =>          true,
            "capability_type"       =>          "post",
            "map_meta_cap"          =>          true,



            // capabilities => array()

            "rewrite"               => array(

                    "slug"              =>          "sponsors",
                    "whit_front"        =>          true,  
                    "pages"             =>          true,
                    "feeds"             =>          true,    

                ),
            "supports"              => array("title","thumbnail")


    );

    register_post_type("sponsors",$args_sponsors);  
}

add_action( 'init', 'eleva_sponsors_custom_post_type' );

function custom_flush_rules(){
    //defines the post type so the rules can be flushed.
    eleva_sponsors_custom_post_type();

    //and flush the rules.
    flush_rewrite_rules();
}

register_activation_hook(__FILE__, 'custom_flush_rules');


?>

1 个答案:

答案 0 :(得分:0)

试试这段代码。

    function eleva_sponsors_custom_post_type() {

        $singular_sponsors = "Sponsor1";

        $plural_sponsors = "Sponsors1";

        $labels_sponsors = array(

                "name"                  =>        $plural_sponsors,
                "singular_name"         =>        $singular_sponsors,
                "add_new"               =>        "Add New",
                "add_new_item"          =>        "Add New " . $singular_sponsors,
                "edit_item"             =>        "Edit " . $singular_sponsors,
                "new_item"              =>        "New " . $singular_sponsors,
                "view_item"             =>        "View " . $singular_sponsors,
                "search_items"          =>        "Search " . $plural_sponsors,
                "not_found"             =>        "No " . $plural_sponsors." found",
                "not_found_in_trash"    =>        "No " .$plural_sponsors." found in trash",
                "all_items"             =>         "All sponsors"
        );

        $args_sponsors = array(

                "labels"                =>          $labels_sponsors,
                "public"                =>          true,
                "publicity queryable"   =>          true,
                "show_in_nav_menus"     =>          true,
                "show_ui"               =>          true,
                "show_in_menu"          =>          true,
                "show_in_admin_bar"     =>          true,
                "menu_position"         =>          10,
                "menu_icon"             =>          "dashicons-format-gallery",
                "can_export"            =>          true,
                "delete_with_user"      =>          false,
                "hierarchical"          =>          false,
                "has_archive"           =>          false,
                "query_bar"             =>          true,
                "capability_type"       =>          "post",
                "map_meta_cap"          =>          true,



                // capabilities => array()

                "rewrite"               => array(
                    "slug"              =>          "sponsors",
                        "whit_front"        =>          true,  
                        "pages"             =>          true,
                        "feeds"             =>          true,    

                    ),
                "supports"              => array("title","thumbnail")


        );

        register_post_type("sponsors2",$args_sponsors);
        flush_rewrite_rules();
    }

    add_action( 'init', 'eleva_sponsors_custom_post_type' );