WordPress自定义帖子类型预览不起作用



自定义帖子类型

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

/*刷新永久链接*/

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

保存永久链接对没有帮助

但没办法!

你能帮忙吗?

我已经测试了您的代码,一切都很好,但是,我想添加一些信息,以帮助您理解问题。

当你的帖子发布时,预览网址将是这样的:

https://dev.test/cars/test/?preview_id=113&preview_nonce=6cf651d710&preview=true

当你的帖子没有发布时,预览url会是这样的,因为它没有发布,它不会给你一个永久链接,

https://dev.test/?post_type=news&p=115&preview=true

所以,如果你尝试https://dev.test/cars/115/?preview=true的起草职位,它肯定会给你404。

在你的例子中,我猜22是你的帖子ID,当你使用https://example.com/cars/22/?preview=true而不发布帖子时,你会得到404,这是对的。

当你发布帖子并使用https://example.com/cars/22/test?preview=true时,它会将你重定向到https://example.com/cars/test,这也是正确的。

因此,您的代码一切正常,没有任何错误。

结论

如果你需要预览一篇未发布的帖子,你必须使用https://example.com?post_type=news&p=22&preview=true,这个url模式。

添加此代码后,它一直在工作。

add_filter('post_type_link', 'prowpsite_change_post_type_link', 1, 3);
function prowpsite_change_post_type_link($link, $post = 0)
{
if ($post->post_type == 'cars' && (strpos($link, "preview") !== false)) {
return home_url('cars/' . $post->ID . '/' .  $post->post_name);
} else {
return $link;
}
}

相关内容

最新更新