簡體   English   中英

WordPress 自定義帖子類型預覽不起作用

[英]WordPress custom post type preview not working

自定義帖子類型

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

保存永久鏈接無濟於事

但是沒辦法!

你能幫我嗎?

我已經測試了您的代碼並且一切正常,但是,我想添加一些可能有助於您理解問題的信息。

當您的帖子發布時,預覽 url 將如下所示:

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 ,此 Z572D4E421E5E6B02711D815E 模式。

自添加此代碼以來,它一直在工作。

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