我正在尝试使用WordPress循环提取和传递数据,并将其解析为动态创建的XML文件。数据显示在创建XML文件的PHP文件上,但在XML文件本身上返回空白。任何想法为什么会这样。唯一可见的值是手动插入的。
在PHP文件上: https://precinctdaycare.com/sitemap-2.php
在XML文件上: https://precinctdaycare.com/movies_list.xml
我的代码如下所示。
<?php
define( 'WP_USE_THEMES', false );
require( './wp-load.php' );
query_posts('showposts=1');
require_once( ABSPATH . 'wp-config.php' );
// the query
$args = array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1);
$wpb_all_query = new \WP_Query($args); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
$url = the_permalink();
$dom = new DOMDocument();
$newUrl = $url;
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$xml_file_name = 'movies_list.xml';
$root = $dom->createElement('url');
$movie_node = $dom->createElement('movie');
$attr_movie_id = new DOMAttr('movie_id', the_permalink());
$movie_node->setAttributeNode($attr_movie_id);
$child_node_title = $dom->createElement('Title', $newUrl);
$movie_node->appendChild($child_node_title);
$child_node_year = $dom->createElement('Year', $newUrl);
$movie_node->appendChild($child_node_year);
$child_node_genre = $dom->createElement('Genre', 'The Campaign');
$movie_node->appendChild($child_node_genre);
$child_node_ratings = $dom->createElement('Ratings', 6.2);
$movie_node->appendChild($child_node_ratings);
$root->appendChild($movie_node);
$dom->appendChild($root);
$dom->save($xml_file_name);
echo "$xml_file_name has been successfully created";
endwhile;
wp_reset_postdata();else : ?>
<?php endif; ?>
<?php echo "</urlset>" ?>
?>