首页 文章

自定义页面在Wordpress中给出404错误 Headers

提问于
浏览
2

我正在运行一个由WordPress驱动的网站,有额外的页面......要将这些页面与WordPress主题集成,我使用以下代码:

<?php
$blog_longd='Title'; // page title
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
get_header();
?>

html code

<?php
get_sidebar();
get_footer();
?>

这工作正常,但页面 Headers 始终显示404错误页面(不是“ Headers ”) .

似乎$ wp-query-> is_404始终设置为true . 我试过覆盖这个值,但它似乎不起作用 . 我尝试通过将 Headers 状态200置于函数get_header()之上来修复它 . 它也不起作用 .

有什么建议?谢谢

3 回答

  • 0

    我知道你问了很长时间但是我遇到了问题,这就是解决方案 .

    <?php
    require('./wp-config.php');
    
    $wp->init();
    $wp->parse_request();
    $wp->query_posts();
    $wp->register_globals();
    $wp->send_headers();
    
    get_header();
    
    echo "HELLO WORLD";
    
    get_footer();
    ?>
    
  • 1

    也许笨拙,但如果你实现了 wp_title 过滤器,你可以将 Headers 更改为你想要的 . 您可以将此代码添加到每个自定义页面的 Headers 中:

    add_filter('wp_title', 'replace_title');
    function replace_title() {
       return 'My new title';
    }
    

    如果你想让它更清洁,可以在插件中使用这个过滤器的更智能版本,并在页面中只设置全局变量(此处为 $override_title ):

    add_filter('wp_title', 'replace_title_if_global');
    function replace_title_if_global($title) {
       global $override_title;
       if ($override_title) {
          return $override_title;
       }
       return $title;
    }
    
  • 3

    文件class-wp.php中有代码:

    function handle_404() {
    ...
        // Don't 404 for these queries if they matched an object.
        if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
            status_header( 200 );
            return;
        }
    ...
    }
    

    处理各种页面的404状态 .

    这段代码的功能堆栈是:

    1) wp-blog-header.php:14, require()
    2) function.php:775, wp()
    3) class-wp.php:525, WP->main()
    4) class-wp.php:491, handle_404()
    

    所以你有两种方法来处理这种情况:

    1)

    require('wp-blog-header.php');
    function status_header( 200 );
    

    2)更正确的是在这里插入你自己的功能

    if ( your_own_function() || ((is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object()) ) {
    

    请求自定义页面时返回 true

相关问题