duanniying2342 2015-11-13 08:45
浏览 76
已采纳

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

I have an interesting task. I've wrote custom 404 handler for wordpress and extracted an URL. Than, after doing some logic I've got a wordpress post ID that I need to display instead of 404 page.

How can I display a wordpress post page instead of 404 page? The only thing I can think of is do

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

But is there any alternative way to do that? Thanks

  • 写回答

3条回答 默认 最新

  • dongmu1390 2015-11-16 15:20
    关注

    I was not able to do this by rewriting template's 404.php. And also I think that is would very template-depended. Instead I've managed to display a post by using template_redirect action. The code of plugin function looks like this:

        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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?