【发布时间】:2022-09-25 21:01:06
【问题描述】:
自定义帖子类型
function prowpsite_create_custom_post_types()
{
$types = array(
// Where the magic happens
array(
\'the_type\' => \'news\',
\'single\' => \'car\',
\'plural\' => \'cars\',
\'rewrite\' => \'cars\',
\'icon\' => \'dashicons-admin-site-alt\',
),
);
foreach ($types as $type) {
$the_type = $type[\'the_type\'];
$single = $type[\'single\'];
$plural = $type[\'plural\'];
$rewrite = $type[\'rewrite\'];
$icon = $type[\'icon\'];
$labels = array(
\'name\' => _x($plural, \'post type general name\'),
\'singular_name\' => _x($single, \'post type singular name\'),
\'add_new\' => _x(\'add\' . $type[\'single\'], $single),
\'add_new_item\' => __(\'Add New \' . $single),
\'edit_item\' => __(\'Edit \' . $single),
\'new_item\' => __(\'New \' . $single),
\'view_item\' => __(\'View \' . $single),
\'search_items\' => __(\'Search \' . $plural),
\'not_found\' => __(\'No \' . $plural . \' found\'),
\'not_found_in_trash\' => __(\'No \' . $plural . \' found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_rest\' => true, // To use Gutenberg editor.
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => 5,
\'block-editor\' => true,
\'rewrite\' => array(\'slug\' => $rewrite),
\'supports\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'custom-fields\', \'excerpt\', \'revisions\'),
\'menu_icon\' => $icon,
);
register_post_type($the_type, $args);
}
}
add_action(\'init\', \'prowpsite_create_custom_post_types\');
/* Flush permalinks */
function prowpsite_theme_rewrite_flush()
{flush_rewrite_rules();
}
add_action(\'init\', \'prowpsite_theme_rewrite_flush\');`
为什么我无法预览自定义帖子类型“汽车”,预览链接返回 404!
https://example.com/cars/22/?preview=true
只有当它发布并且链接有这样的蛞蝓时它才有效!
https://example.com/cars/22/test?preview=true
我该如何解决?
尝试使用
add_filter(\'preview_post_link\', \'bitflower_change_post_link\', 10, 2);
也试过
add_filter(\'preview_post_car_link\', \'bitflower_change_post_link\', 10, 2);
保存永久链接无济于事
但是没办法!
你能帮我吗?
-
转到永久链接设置页面并重新保存设置然后测试
-
@VijayHardaha 谢谢,但它不能解决任何问题!我正在使用函数 ephemeris_theme_rewrite_flush() { flush_rewrite_rules(); } add_action(\'init\', \'ephemeris_theme_rewrite_flush\');
-
请分享您用于注册自定义帖子类型的完整代码,如果您添加了任何额外的重写规则或其他类似内容,请在您的问题中添加,并解释您做了什么以及为什么这样做。
-
我已经添加了完整的代码
-
您的代码中有两次
rewrite,能否请您删除\'rewrite\' => true,,然后重新保存固定链接?网址中的22是什么?任何想法?
标签: wordpress http-status-code-404 customization permalinks