$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Can I change the page of a Custom Post Type slug?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Can I change the page of a Custom Post Type slug?

matteradmin10PV0评论

So I've created a simple custom post type for separating content in the back-end. The Custom Post Type has a slug and generates a URL using this slug, such as www.yoursite/slug/. Any pages made in the custom posttype will be yoursite/slug/page-name. This is correct, but now I wish to change the content of the page on yoursite/slug/. Everytime I visit this page it will redirect me to the homepage. Is there any way to change this?

I forgot to mention I've used the plugin CPT UI. I found this code in CPT UI, not sure if it is of any use.

function cptui_register_my_cpts_kennisbank() {

    /**
     * Post Type: Kennisbank.
     */

    $labels = array(
        "name" => __( "Kennisbank", "blankslate" ),
        "singular_name" => __( "Kennisbank", "blankslate" ),
    );

    $args = array(
        "label" => __( "Kennisbank", "blankslate" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "delete_with_user" => false,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "kennisbank", "with_front" => true ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail" ),
    );

    register_post_type( "kennisbank", $args );
}

add_action( 'init', 'cptui_register_my_cpts_kennisbank' );

So I've created a simple custom post type for separating content in the back-end. The Custom Post Type has a slug and generates a URL using this slug, such as www.yoursite/slug/. Any pages made in the custom posttype will be yoursite/slug/page-name. This is correct, but now I wish to change the content of the page on yoursite/slug/. Everytime I visit this page it will redirect me to the homepage. Is there any way to change this?

I forgot to mention I've used the plugin CPT UI. I found this code in CPT UI, not sure if it is of any use.

function cptui_register_my_cpts_kennisbank() {

    /**
     * Post Type: Kennisbank.
     */

    $labels = array(
        "name" => __( "Kennisbank", "blankslate" ),
        "singular_name" => __( "Kennisbank", "blankslate" ),
    );

    $args = array(
        "label" => __( "Kennisbank", "blankslate" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "delete_with_user" => false,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "kennisbank", "with_front" => true ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail" ),
    );

    register_post_type( "kennisbank", $args );
}

add_action( 'init', 'cptui_register_my_cpts_kennisbank' );
Share Improve this question edited Jan 5, 2019 at 8:26 Nisse Engström 1151 silver badge2 bronze badges asked Dec 25, 2018 at 14:39 MelvinMelvin 32 bronze badges 5
  • Please edit your question and add the code you used to register your post type. – Milo Commented Dec 25, 2018 at 15:32
  • yes Milo is right you used register_post_type('example', $args); like this – vikrant zilpe Commented Dec 26, 2018 at 5:45
  • Code? I used the plugin CPT UI. – Melvin Commented Dec 26, 2018 at 10:56
  • If I understand your question correctly, you want to override /slug/? If so, you can just create a standard page title Slug and it will automatically create a post_name value of "slug" which should work fine to be at /slug/ (so long as your standard permalinks are %postname% or %category%/%postname%) – majick Commented Dec 26, 2018 at 13:01
  • What I’d like to do is edit the page at /slug/ indeed, however there is not really a page I can edit anywhere. I havn’t tried creating a page with the same slug just yet – Melvin Commented Dec 26, 2018 at 14:51
Add a comment  | 

1 Answer 1

Reset to default 1

Custom post types can have a post type archive, which displays all posts of that type. In your post type registration code, it's controlled by the has_archive argument, which is now currently false. Change it to true, or give it a string value to set it to something specific other than your post type slug.

"has_archive" => "kennisbank",
Post a comment

comment list (0)

  1. No comments so far