wordpress自定义帖子类型返回404

时间:2019-09-04 11:12:30

标签: php wordpress permalinks

我已经创建了这样的自定义帖子类型,

add_action( 'init', 'used_cars_cpt' );

function 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,这是我唯一可以查看的帖子,是当我使用普通的永久链接但我想使用{{1} }。任何人都可以指出为什么我的固定链接上的帖子类型为404ing?

我尝试刷新永久链接。

4 个答案:

答案 0 :(得分:0)

使用此代码

/*
* 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 );

您必须在更改代码后更新永久链接。

更多信息,here

答案 1 :(得分: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,
  )
);

?>

别忘了再次保存永久链接。

答案 2 :(得分:0)

这将起作用。并且还可以尝试通过更改设置中的固定链接

    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' );

答案 3 :(得分:0)

只需转到设置->永久链接 如果尚未设置,则选择发布名称选项,如果已经将其设置为该选项,则在两种情况下均单击保存按钮。 它将再次更新您的htaccess,并且永久链接将起作用。

相关问题