第 1 行第 1 列错误:文档为空 - header('Content-Type: text/xml; charset=UTF-8');

Error on line 1 at column 1: Document is empty - header('Content-Type: text/xml; charset=UTF-8');

您好,我的 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 上没有出现错误,但所有 links 都出现在 WordPress 的管理页面上。我的错误在哪里?

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

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

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

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

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