【问题标题】:Error on line 1 at column 1: Document is empty - header('Content-Type: text/xml; charset=UTF-8');第 1 列第 1 行的错误:文档为空 - header('Content-Type: text/xml; charset=UTF-8');
【发布时间】:2021-02-03 04:41:48
【问题描述】:

您好,我的插件 WordPress 中有此错误,问题是行代码 header('Content-Type: text/xml; charset=UTF-8');

public function listPostByCategory(){
    $misc = 'categories';
    $this->buildSitemap( $misc );
    $this->buildSitemapHeader();

    // get all the categories from the database
    $this->categories = get_categories();
            
    // loop through the categries
    foreach($this->categories as $category) {
        
        // setup the cateogory ID
        $cat_id= $category->term_id;

        // Make a header for the cateogry
        //echo '<h2>' . $category->name . '</h2>';

        // create a custom wordpress 
        $args = array(
            'cat' =>  $cat_id
        );
        
        // The Query
        $the_query = new \WP_Query( $args );
        
        // The Loop
        if ( $the_query->have_posts() ) {
            //echo '<ul>';
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    // echo '<li>' . the_permalink() . '</li>';
                    // echo '<hr/>';
                    $this->url = the_permalink();
                    $this->addLink( $this->url );
                }
            //echo '</ul>';
        }    
        /* Restore original Post Data */
        wp_reset_postdata();
    }

    $this->buildFooterSitemap();
}

构建 XML 文件的函数

private function buildSitemap( $arg )
{
    header('Content-Type: text/xml; charset=UTF-8');

    $filename = '/sitemap-'.$arg.'.xml';
    
    $this->styleSheet = $this->plugin_path . 'xml-sitemap.xls';

    $this->date = wp_date( 'F d, Y h:i:s a' );

    $this->fileSitemap = get_stylesheet_directory() . $filename;

}

private function buildSitemapHeader()
{
    $this->handler = fopen( $this->fileSitemap, "w+" );
    fwrite($this->handler, '<?xml version="1.0" encoding="UTF-8"?>'. "\n");
    fwrite($this->handler, '<?xml-stylesheet type="text/xsl" href="' . esc_url( $this->styleSheet ) . '"?>'. "\n");
    fwrite($this->handler, '<!-- generated-on="'. $this->date .'" -->'. "\n");
    fwrite($this->handler, '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'. "\n");
}

//private function add($url, $mod, $chfreq, $prio)
private function addLink( $url )
{
    fwrite($this->handler, "\t". '<url>' ."\n");
    fwrite($this->handler, "\t\t". '<loc>'.$url.'</loc>' ."\n");
    fwrite($this->handler, "\t". '</url>'. "\n");
}

private function buildFooterSitemap(){
    fwrite($this->handler, '</urlset>');
    fclose($this->handler);
}

如果我评论 header('Content-Type: text/xml; charset=UTF-8');在 function buildSitemap 上没有出现错误,但所有链接都出现在 WordPress 的管理页面上。我的错误在哪里?

如果我传入 $this->addLink( $this->url );一个简单的字符串没有出现错误,并且没有在管理面板中写入任何链接。我不明白为什么 the_permalink() 会导致错误

【问题讨论】:

  • 错误说明了什么?标头已发送?
  • 我写的错误是:第 1 行第 1 列的错误:文档为空。如果我评论 header('Content-Type: text/xml; charset=UTF-8');没有出现错误
  • 当您调用$this-&gt;handler = fopen( $this-&gt;fileSitemap, "w+" ); 时,您是否正在检查文件是否存在,因为fileSitemap 调用正在theme 目录中查找它
  • 特别是只有在我调用 $this->addLink( $this->url ); 时才会出现错误在 Wp_Query 上,我不明白为什么......这个函数创建了 xml 的主体......
  • 啊,我现在明白了,使用get_the_permalink() 而不是the_permalink()the_permalink() 回显链接不返回它,所以$this-&gt;url 始终为空

标签: wordpress plugins header php-7


【解决方案1】:

添加以下行:header('Content-type: text/plain;charset=UTF-8');

您可能会看到动态 wordpress 站点地图,https://github.com/shantun7792/sitemaps/blob/master/sitemap.php

【讨论】:

  • 您好,感谢您的建议。我发现@Josh Bonnick 的解决方案有帮助
【解决方案2】:

the_permalink() 回显该值。所以它永远不会分配给您的 $this-&gt;url 变量。

您需要使用get_the_permalink() 将其分配给一个变量,因为这个函数returns 的值。

【讨论】:

    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 2021-11-08
    • 2023-02-06
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2011-12-18
    相关资源
    最近更新 更多