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

报告相同问题?

悬赏问题

  • ¥20 全书网Java爬取数据
  • ¥15 SAP HANA SQL Script 。SUM OVER 怎么加where
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题