【问题标题】:WordPress custom post type preview not workingWordPress自定义帖子类型预览不起作用
【发布时间】: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


【解决方案1】:

我已经测试了您的代码并且一切正常,但是,我想添加一些可能有助于您理解问题的信息。

当您的帖子发布后,预览网址将如下所示:

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

当您的帖子未发布时,预览网址将是这样的,因为它没有发布它不会给您一个永久链接,

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 模式。

【讨论】:

  • 这意味着我每次都必须手动编辑 URL,即使我测试了 dev.test/cars/test/?preview_id=113&preview=true 之类的 URL 但无法正常工作!
  • 您在后端有一个预览按钮,您无需手动输入和打开 url。 wordpress 将在具有有效 url 参数的新选项卡中打开预览。
  • 预览按钮链接显示dev.test/cars/22&preview=true。不像你说的
  • 恐怕您已经添加了某种自定义,并且您没有明确说明您的问题。但是,如果您单击预览更改在经典编辑器上或预习在块编辑器中,默认情况下,您将在 url 中拥有预览 ID 和 nonce。如果您已经进行了一些自定义,那么它们如何处理它取决于您的自定义。
【解决方案2】:

自添加此代码以来,它一直在工作。

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

【讨论】:

    猜你喜欢
    • 2013-06-02
    • 2013-11-02
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2012-10-26
    相关资源
    最近更新 更多