【问题标题】:custom pages give 404 error title in Wordpress自定义页面在 Wordpress 中给出 404 错误标题
【发布时间】:2011-03-27 18:51:00
【问题描述】:

我正在运行一个由 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();
?>

这很好用,但是页面标题总是显示 404 错误页面(不是“标题”)。

似乎 $wp-query->is_404 总是设置为 true。我尝试覆盖这个值,但它似乎不起作用。我尝试通过将标题状态 200 放在函数 get_header() 上方来修复它。但它也不起作用。

有什么建议吗? 谢谢

【问题讨论】:

    标签: wordpress http-status-code-404 page-title


    【解决方案1】:

    我知道你已经很久没有问过了,但我遇到了问题,这里是解决方案。

    <?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();
    ?>
    

    【讨论】:

      【解决方案2】:

      可能有点笨拙,但如果你实现了wp_title 过滤器,你可以将标题更改为你想要的。您可以将此代码添加到每个自定义页面的标题中:

      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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-06
          • 1970-01-01
          • 2016-04-25
          • 2013-09-16
          • 2014-04-17
          • 2019-10-29
          • 2013-05-29
          相关资源
          最近更新 更多