簡體   English   中英

wordpress自定義帖子類型返回404

[英]wordpress custom post type returns 404

我創建了這樣的自定義帖子類型,

add_action( 'init', 'used_cars_cpt' );

函數used_cars_cpt(){

register_post_type('used-cars', [
    'labels' => [
        'name' => 'Used Cars',
        'singular_name' => 'Used Car',
    ],
    'rewrite' => array('slug' => 'cars'),
    'description' => '',
    'public' => true,
    'menu_position' => 20,
    'supports' => ['custom-fields']
]);

}

我可以使用這種帖子類型創建帖子,但是當我嘗試查看該帖子時,我得到的是404,這是我唯一可以查看的帖子,是當我使用普通的永久鏈接,但我想使用/cars/some-kind-of-post-title 任何人都可以指出為什么我的固定鏈接上的帖子類型為404ing嗎?

我試過沖洗永久鏈接。

使用此代碼

/*
* Creating a function to create our CPT
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => 'Used Cars',
        'singular_name'       => 'Used Cars',
        'menu_name'           => 'Used Cars',
        'parent_item_colon'   => __( 'Parent Used Cars', 'twentythirteen' ),
        'all_items'           => __( 'All Used Cars', 'twentythirteen' ),
        'view_item'           => __( 'View Used Cars', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Used Cars', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Used Cars', 'twentythirteen' ),
        'update_item'         => __( 'Update Used Cars', 'twentythirteen' ),
        'search_items'        => __( 'Search Used Cars', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'Used Cars', 'twentythirteen' ),
        'description'         => __( 'Used Cars desc', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'cars', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );

您必須在更改代碼后更新永久鏈接。

更多信息在這里

Please use this:

    <?php

function cars() {
  register_post_type('cars',
    array(
      'labels' => array(
        'name' => __('Cars', 'domain_name'),
        'singular_name' => __('Car', 'domain_name'),
        'all_items' => __('All Cars CPTs', 'domain_name'),
        'add_new' => __('Add New Car', 'domain_name'),
        'add_new_item' => __('Add New Car', 'domain_name'),
        'edit' => __('Edit', 'domain_name'),
        'edit_item' => __('Edit Car', 'domain_name'),
        'new_item' => __('New Car', 'domain_name'),
        'view_item' => __('View Car', 'domain_name'),
        'search_items' => __('Search Car', 'domain_name'),
        'not_found' =>  __('No Car found. Add some and they\'ll appear here!', 'domain_name'),
        'not_found_in_trash' => __('Deleted Cars will appear here.', 'domain_name'),
        'parent_item_colon' => ''
      ),

      'description' => __('cars', 'domain_name'),
      'public' => true,
      'publicly_queryable' => true,
      'exclude_from_search' => false,
      'show_ui' => true,
      'query_var' => true,
      'menu_position' => 8,
      'menu_icon' => 'dashicons-media-default',
      'rewrite' => array(
        'slug' => 'cars',
        'with_front' => false 
      ),

      'has_archive' => 'cars',
      'capability_type' => 'post',
      'hierarchical' => false,
      'supports' => array(
        'title',
        'editor',
        'author',
        'page-attributes',
        'thumbnail',
        'excerpt',
        'trackbacks',
        'custom-fields',
        'comments',
        'revisions',
        'sticky'
      )
    )
  );

  register_taxonomy_for_object_type('cars-category', 'cars');
  register_taxonomy_for_object_type('cars-tag', 'cars');
}

add_action('init', 'cars');

register_taxonomy( 'cars-category', 
  array('cars'),
  array(
    'hierarchical' => true,
    'labels' => array(
      'name' => __('Categories', 'domain_name'),
      'singular_name' => __('Category', 'domain_name'),
      'search_items' =>  __('Search Categories', 'domain_name'),
      'all_items' => __('All Categories', 'domain_name'),
      'parent_item' => __('Parent Category', 'domain_name'),
      'parent_item_colon' => __('Parent Category:', 'domain_name'),
      'edit_item' => __('Edit Category', 'domain_name'),
      'update_item' => __('Update Category', 'domain_name'),
      'add_new_item' => __('Add New Category', 'domain_name'),
      'new_item_name' => __('New Category Name', 'domain_name')
    ),

    'show_admin_column' => true, 
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'cars-categories' ),
  )
);

register_taxonomy('cars-tag', 
  array('cars'),
  array(
    'hierarchical' => false,
    'labels' => array(
      'name' => __('Tags', 'domain_name'),
      'singular_name' => __('Tag', 'domain_name'),
      'search_items' =>  __('Search Tags', 'domain_name'),
      'all_items' => __('All Tags', 'domain_name'),
      'parent_item' => __('Parent Tag', 'domain_name'),
      'parent_item_colon' => __('Parent Tag:', 'domain_name'),
      'edit_item' => __('Edit Tag', 'domain_name'),
      'update_item' => __('Update Tag', 'domain_name'),
      'add_new_item' => __('Add New Tag', 'domain_name'),
      'new_item_name' => __('New Tag Name', 'domain_name')
    ),

    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
  )
);

?>

不要忘記再次保存永久鏈接。

這將起作用。 還可以嘗試通過從設置更改固定鏈接

    function used_cars_cpt() {

    $args = array(
        'labels' => [
        'name' => 'Used Cars',
        'singular_name' => 'Used Car',
    ],
    'rewrite' => array('slug' => 'cars'),
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'description' => '',
    'public' => true,
    'capability_type'    => 'post',
    'menu_position' => 20,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );

    register_post_type( 'used-cars', $args );
}

add_action( 'init', 'used_cars_cpt' );

如果尚未設置,請轉到設置->永久鏈接,選擇發布名稱選項,如果已經將其設置為該選項,則在兩種情況下都單擊保存按鈕。 它將再次更新您的htaccess,並且永久鏈接將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM