【问题标题】:Hook for redirection on wordpress钩子在 wordpress 上重定向
【发布时间】:2021-11-08 05:03:24
【问题描述】:

我正在 wordpress 上制作自定义主题。而不是安装插件,我想知道如何使用functions.php 文件中的挂钩进行404 重定向。

如果用户访问“my-domain.com/page/sub-page”,我如何在此 url 上获得 404 页面?

我在自定义主题中创建了一个404.php 页面。

【问题讨论】:

    标签: php wordpress redirect wordpress-theming http-status-code-404


    【解决方案1】:

    好的,我有答案了:

    add_action('init', function() {
        if ( strpos($_SERVER['REQUEST_URI'], '/page/sub-page/') !== false ) 
        {
            wp_redirect('/404/');
            exit;
        }
    });
    

    另一种解决方案:

    add_action('init', function() {
        if ( strpos($_SERVER['REQUEST_URI'], '/page/sub-page/') !== false ) {
            status_header(404);
            get_template_part(404);
            exit();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多