自定义 Post 类型单页显示 404 错误
Custom Post Type Single page shows 404 error
我正在使用自定义 Post 类型永久链接
通过 Toro_Unit 用于自定义 url 的插件。
这是我的自定义 post 类型
<?php
/**
* Post Type: our-products.
*/
function cptui_register_my_cpts_our_products() {
$labels = array(
"name" => __("Our Products", "twentynineteen"),
"singular_name" => __("Product", "twentynineteen"),
"menu_name" => __("Products", "twentynineteen"),
"all_items" => __("Products", "twentynineteen"),
"add_new" => __("Add Products", "twentynineteen"),
"add_new_item" => __("Add New Product", "twentynineteen"),
"edit_item" => __("Edit Product", "twentynineteen"),
"new_item" => __("New Product", "twentynineteen"),
"view_item" => __("View Product", "twentynineteen"),
"view_items" => __("View Products", "twentynineteen"),
"search_items" => __("Search Products", "twentynineteen"),
"not_found" => __("No Product Found", "twentynineteen"),
"not_found_in_trash" => __("No Product Found in Trash", "twentynineteen"),
"parent_item_colon" => __("Parent Product", "twentynineteen"),
"featured_image" => __("Featured Image", "twentynineteen"),
"set_featured_image" => __("Set Featured Image for Product", "twentynineteen"),
"remove_featured_image" => __("Remove Featured Image for Product", "twentynineteen"),
"use_featured_image" => __("Use as Featured Image For Product", "twentynineteen"),
"archives" => __("Product Archieves", "twentynineteen"),
"insert_into_item" => __("Insert into Products", "twentynineteen"),
"uploaded_to_this_item" => __("Upload to this product", "twentynineteen"),
"filter_items_list" => __("Filter Product List", "twentynineteen"),
"items_list_navigation" => __("Filter Product List Navigation", "twentynineteen"),
"items_list" => __("Products List", "twentynineteen"),
"attributes" => __("Product Attributes", "twentynineteen"),
"name_admin_bar" => __("Products", "twentynineteen"),
"parent_item_colon" => __("Parent Product", "twentynineteen"),
);
$args = array(
"label" => __("our-products", "twentynineteen"),
"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" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array("slug" => "our-products", "with_front" => true),
"query_var" => "our-products",
"supports" => array("title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "page-attributes", "post-formats"),
);
register_post_type("our-products", $args);
}
add_action('init', 'cptui_register_my_cpts_our_products');
function wpdocs_register_private_taxonomy1() {
$args = array(
'label' => __('Product Category', 'textdomain'),
'public' => true,
"publicly_queryable" => true,
"show_in_rest" => true,
'rewrite' => true,
'hierarchical' => true,
'has_archive' => true
);
register_taxonomy('product-category', 'our-products', $args);
$labels = array(
'name' => _x('Tags', 'taxonomy general name'),
'singular_name' => _x('Tag', 'taxonomy singular name'),
'search_items' => __('Search Tags'),
'popular_items' => __('Popular Tags'),
'all_items' => __('All Tags'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit Tag'),
'update_item' => __('Update Tag'),
'add_new_item' => __('Add New Tag'),
'new_item_name' => __('New Tag Name'),
'separate_items_with_commas' => __('Separate tags with commas'),
'add_or_remove_items' => __('Add or remove tags'),
'choose_from_most_used' => __('Choose from the most used tags'),
'menu_name' => __('Tags'),
);
register_taxonomy('tag', 'our-products', array(// replace your post type with "news"
'hierarchical' => false,
'labels' => $labels,
"show_in_rest" => true,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'tag'),
));
}
add_action('init', 'wpdocs_register_private_taxonomy1', 0);
flush_rewrite_rules( false );
我想制作自定义分类的 url
site_url/our-products/custom-taxonomy-name
单页
site_url/our-products/custom-taxomony-name/single-page-url
所以设置的时候
Url 工作正常。但它显示 404 错误。
试过:根本不起作用
设置刷新的永久链接-->永久链接-->自定义结构-->保存
flush_rewrite_rules( false ) 在 functions.php 和创建时
自定义 post 类型
已提供解决方案here
也尝试了this解决方案
.htaccess 文件:检查没有问题。
- 在经历了所有的挫折之后,安装了一个新的设置,创建了一个自定义 post 类型并将永久链接设置为 /%category%/%postname%/ ,而不是
为自定义 post 类型工作。
仅供参考,我使用的是wordpress 5.3.1,与主题或插件没有冲突。已测试。
注册自定义 post 类型时
将此添加到您的自定义 post 类型
"taxonomies" => [ "category", "product-category" ],
当时有效
问题出在这一行:
register_taxonomy('tag', 'our-products', array(
当您使用“tag”关键字时,事情会发生冲突。
您可以重命名您的自定义分类法,但您将丢失已添加的术语。
您可以使用自定义 query_var 参数来解决这个问题,不要将其设置为 true。该参数还接受一个字符串:
'query_var' => 'a'
我正在使用自定义 Post 类型永久链接 通过 Toro_Unit 用于自定义 url 的插件。
这是我的自定义 post 类型
<?php
/**
* Post Type: our-products.
*/
function cptui_register_my_cpts_our_products() {
$labels = array(
"name" => __("Our Products", "twentynineteen"),
"singular_name" => __("Product", "twentynineteen"),
"menu_name" => __("Products", "twentynineteen"),
"all_items" => __("Products", "twentynineteen"),
"add_new" => __("Add Products", "twentynineteen"),
"add_new_item" => __("Add New Product", "twentynineteen"),
"edit_item" => __("Edit Product", "twentynineteen"),
"new_item" => __("New Product", "twentynineteen"),
"view_item" => __("View Product", "twentynineteen"),
"view_items" => __("View Products", "twentynineteen"),
"search_items" => __("Search Products", "twentynineteen"),
"not_found" => __("No Product Found", "twentynineteen"),
"not_found_in_trash" => __("No Product Found in Trash", "twentynineteen"),
"parent_item_colon" => __("Parent Product", "twentynineteen"),
"featured_image" => __("Featured Image", "twentynineteen"),
"set_featured_image" => __("Set Featured Image for Product", "twentynineteen"),
"remove_featured_image" => __("Remove Featured Image for Product", "twentynineteen"),
"use_featured_image" => __("Use as Featured Image For Product", "twentynineteen"),
"archives" => __("Product Archieves", "twentynineteen"),
"insert_into_item" => __("Insert into Products", "twentynineteen"),
"uploaded_to_this_item" => __("Upload to this product", "twentynineteen"),
"filter_items_list" => __("Filter Product List", "twentynineteen"),
"items_list_navigation" => __("Filter Product List Navigation", "twentynineteen"),
"items_list" => __("Products List", "twentynineteen"),
"attributes" => __("Product Attributes", "twentynineteen"),
"name_admin_bar" => __("Products", "twentynineteen"),
"parent_item_colon" => __("Parent Product", "twentynineteen"),
);
$args = array(
"label" => __("our-products", "twentynineteen"),
"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" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array("slug" => "our-products", "with_front" => true),
"query_var" => "our-products",
"supports" => array("title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "page-attributes", "post-formats"),
);
register_post_type("our-products", $args);
}
add_action('init', 'cptui_register_my_cpts_our_products');
function wpdocs_register_private_taxonomy1() {
$args = array(
'label' => __('Product Category', 'textdomain'),
'public' => true,
"publicly_queryable" => true,
"show_in_rest" => true,
'rewrite' => true,
'hierarchical' => true,
'has_archive' => true
);
register_taxonomy('product-category', 'our-products', $args);
$labels = array(
'name' => _x('Tags', 'taxonomy general name'),
'singular_name' => _x('Tag', 'taxonomy singular name'),
'search_items' => __('Search Tags'),
'popular_items' => __('Popular Tags'),
'all_items' => __('All Tags'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit Tag'),
'update_item' => __('Update Tag'),
'add_new_item' => __('Add New Tag'),
'new_item_name' => __('New Tag Name'),
'separate_items_with_commas' => __('Separate tags with commas'),
'add_or_remove_items' => __('Add or remove tags'),
'choose_from_most_used' => __('Choose from the most used tags'),
'menu_name' => __('Tags'),
);
register_taxonomy('tag', 'our-products', array(// replace your post type with "news"
'hierarchical' => false,
'labels' => $labels,
"show_in_rest" => true,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'tag'),
));
}
add_action('init', 'wpdocs_register_private_taxonomy1', 0);
flush_rewrite_rules( false );
我想制作自定义分类的 url
site_url/our-products/custom-taxonomy-name
单页
site_url/our-products/custom-taxomony-name/single-page-url
所以设置的时候
Url 工作正常。但它显示 404 错误。
试过:根本不起作用
设置刷新的永久链接-->永久链接-->自定义结构-->保存
flush_rewrite_rules( false ) 在 functions.php 和创建时 自定义 post 类型
已提供解决方案here
也尝试了this解决方案
.htaccess 文件:检查没有问题。
- 在经历了所有的挫折之后,安装了一个新的设置,创建了一个自定义 post 类型并将永久链接设置为 /%category%/%postname%/ ,而不是 为自定义 post 类型工作。
仅供参考,我使用的是wordpress 5.3.1,与主题或插件没有冲突。已测试。
注册自定义 post 类型时 将此添加到您的自定义 post 类型
"taxonomies" => [ "category", "product-category" ],
当时有效
问题出在这一行:
register_taxonomy('tag', 'our-products', array(
当您使用“tag”关键字时,事情会发生冲突。
您可以重命名您的自定义分类法,但您将丢失已添加的术语。
您可以使用自定义 query_var 参数来解决这个问题,不要将其设置为 true。该参数还接受一个字符串:
'query_var' => 'a'