这不是我问题的解决方案,但它帮助我在Wordpress中解决我的问题,我想与大家分享。
我使用XMLWriter创建了一个单独的站点地图。这是代码。
Wordpress挂钩:
add_action(\'parse_request\', \'my_custom_url_handler\');
function my_custom_url_handler() {
$c = new EventsSitemapBuilder();
if( $_SERVER["REQUEST_URI"] == \'/website/events-sitemap.xml\') {
header("Content-type: text/xml");
$c = new EventsSitemapBuilder();
$c->build();
}
exit;
}
EventsSitemapBuilder():
class EventsSitemapBuilder {
private $api_client;
private $sitemap_data;
private $sm_xml_writer;
public function __construct() {
$this->api_client = new ALApiClient();
$this->sitemap_data = $this->api_client->sitemap_data();
$this->sm_xml_writer = new XMLWriter();
}
public function build() {
//events.id, it_texts.slug AS it_slug, en_texts.slug AS en_slug, events.updated_at
//$this->sm_xml_writer->openUri( home_url( "events-sitemap" ) );
$this->sm_xml_writer->openUri( "php://output" );
$this->sm_xml_writer->startDocument(\'1.0\', \'UTF-8\');
$this->sm_xml_writer->startElement(\'urlset\');
$this->sm_xml_writer->writeAttribute(\'xmlns\', "http://www.sitemaps.org/schemas/sitemap/0.9");
array_map( array( $this, \'sitemap_event_entry\' ), $this->sitemap_data );
$this->sm_xml_writer->endElement();
$this->sm_xml_writer->endDocument();
$this->sm_xml_writer->flush();
}
private function sitemap_event_entry( $data ) {
$this->sm_xml_writer->startElement(\'url\');
$this->sm_xml_writer->writeElement(\'lastmod\', $data[\'date\']);
$this->sm_xml_writer->writeElement(\'changefreq\', \'weekly\');
$this->sm_xml_writer->writeElement(\'priority\', 0.6);
$event_url = home_url( \'events/\' . $data[\'id\'] );
$this->sm_xml_writer->writeElement(\'loc\', $data[\'url\'] );
$this->sm_xml_writer->endElement();
}
}