gpt4 book ai didi

php - 如何显示 wordpress 帖子而不是 404 页面?

转载 作者:行者123 更新时间:2023-12-05 01:46:45 26 4
gpt4 key购买 nike

我有一个有趣的任务。我已经为 wordpress 编写了自定义 404 处理程序并提取了一个 URL。然后,在执行一些逻辑之后,我得到了一个 wordpress 帖子 ID,我需要显示它而不是 404 页面。

如何显示 wordpress 帖子页面而不是 404 页面?我唯一能想到的就是做

 echo wp_remote_fopen(....<post permalink>...);

但是有没有其他方法可以做到这一点?谢谢

最佳答案

我无法通过重写模板的 404.php 来做到这一点。而且我认为这将非常依赖于模板。相反,我设法通过使用 template_redirect 操作来显示帖子。插件函数的代码如下所示:

    function func_404_redirect($query){
global $wp_query;
$post = get_post(2);
$wp_query->queried_object = $post;
$wp_query->is_single = true;
$wp_query->is_404 = false;
$wp_query->queried_object_id = $post->ID;
$wp_query->post_count = 1;
$wp_query->current_post=-1;
$wp_query->posts = array($post);
}
add_filter('template_redirect','func_404_redirect');

关于php - 如何显示 wordpress 帖子而不是 404 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com