简体   繁体   English

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

[英]WordPress custom post type preview not working

The custom post type自定义帖子类型

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

Why I can't preview the custom post type "car", the preview link return 404!为什么我无法预览自定义帖子类型“汽车”,预览链接返回 404!

https://example.com/cars/22/?preview=true

It works when only it published and the link has the slug like this!!只有当它发布并且链接有这样的蛞蝓时它才有效!

https://example.com/cars/22/test?preview=true

How can I fix it?我该如何解决?

Tried to use尝试使用

add_filter('preview_post_link', 'bitflower_change_post_link', 10, 2);

and also tried也试过

add_filter('preview_post_car_link', 'bitflower_change_post_link', 10, 2);

Saving the permalinks not help保存永久链接无济于事

But no way!但是没办法!

Can you help?你能帮我吗?

I have tested your code and everything works fine, however, I want to add some information that might help your understanding of the problem.我已经测试了您的代码并且一切正常,但是,我想添加一些可能有助于您理解问题的信息。

When your post is published the preview url will be like this:当您的帖子发布时,预览 url 将如下所示:

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

When you're post is not published the preview url will be like this, because it's not published it will not give you a permalink,当您的帖子未发布时,预览 url 将是这样的,因为它没有发布它不会给您永久链接,

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

so if you'll try https://dev.test/cars/115/?preview=true for drafted posts, it will give you 404 for sure.因此,如果您尝试https://dev.test/cars/115/?preview=true起草帖子,它肯定会给您 404。

In your examples, I am guessing 22 is your post ID and when you use https://example.com/cars/22/?preview=true without publishing the post you get 404, which is right.在您的示例中,我猜22是您的帖子 ID,当您使用https://example.com/cars/22/?preview=true而不发布帖子时,您会得到 404,这是正确的。

and when you publish the post and use https://example.com/cars/22/test?preview=true and it will redirect you to https://example.com/cars/test that is also right.当您发布帖子并使用https://example.com/cars/22/test?preview=true时,它会将您重定向到https://example.com/cars/test ,这也是正确的。

So everything is right with your code nothing is wrong.因此,您的代码一切正常,没有任何问题。

CONCLUSION结论

If you need to preview an unpublished post you'll have to use https://example.com?post_type=news&p=22&preview=true , this url pattern.如果您需要预览未发布的帖子,则必须使用https://example.com?post_type=news&p=22&preview=true ,此 Z572D4E421E5E6B02711D815E 模式。

It has been working since adding this code.自添加此代码以来,它一直在工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM