我最近制作了一个站点地图,它可以将我的帖子保存到xml文件中,更改周围的字段,以获取您想要放入其中的内容,但这很可能会解决您的问题。
您要研究的函数是
$fp = fopen(ABSPATH . "sitemap.xml", \'w\');
fwrite($fp, $sitemap);
fclose($fp);
这就是乐趣的开始
add_action("publish_post", "eg_create_sitemap");
add_action("publish_page", "eg_create_sitemap");
function eg_create_sitemap() {
$postsForSitemap = get_posts(array(
\'numberposts\' => -1,
\'orderby\' => \'modified\',
\'post_type\' => array(\'post\'),
\'order\' => \'ASC\',
\'post_status\' => \'publish\'
));
$sitemap = \'<?xml version="1.0" encoding="UTF-8"?>\'."\\n";
$sitemap .= \'<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" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">\'."\\n";
/*$sitemap .= \'<?xml-stylesheet type="text/xsl" href="http://sitehere.com/sitemap.xsl"?>\'."\\n";*/
foreach($postsForSitemap as $post)
{
setup_postdata($post);
$post_id = $post->ID;
$postdat = $post->post_date;
$postdate = explode(" ", $postdat);
$postdatee = $postdate[0]."T".$postdate[1]."-08:00";
$postContent = $post->post_excerpt;
$postName = $post->post_name;
$posttitle = $post->post_title;
$postimg = get_post_meta($post_id, \'image\', true);
$postviews = get_post_meta($post_id, \'views\', true);
$postrating = get_post_meta($post_id, \'ratings_average\', true);
$postCategorys = get_the_category($post_id);
$posttags = get_the_tags($post_id);
$post_contentloc = get_post_meta($post_id, \'enclosure\', true);
$post_content_loc = explode("\\n", $post_contentloc);
$abspath = get_site_url();
$postcat = $postCategorys[0]->category_nicename;
$sitemap .= \'<url>\'."\\n".
"\\t".\'<loc>\'.$abspath.\'/\'.$postcat."/".$postName.\'/</loc>\'."\\n".
"\\t".\'<video:video>\'."\\n".
"\\t\\t".\'<video:thumbnail_loc>\'.$postimg.$post_custom[image].\'</video:thumbnail_loc>\'."\\n".
"\\t\\t".\'<video:title>\'. $posttitle .\'</video:title>\'."\\n".
"\\t\\t".\'<video:publication_date>\'. $postdatee .\'</video:publication_date>\'."\\n".
"\\t\\t".\'<video:description>\'. $postContent .\'</video:description>\'."\\n".
"\\t\\t".\'<video:content_loc>\'.$post_content_loc[0].\'</video:content_loc>\'."\\n".
"\\t\\t".\'<video:view_count>\'. $postviews .\'</video:view_count>\'."\\n".
"\\t\\t".\'<video:rating>\'. $postrating .\'</video:rating>\'."\\n".
"\\t\\t".\'<video:player_loc allow_embed="yes" autoplay="ap=1">http://sitehere.com/wp-content/uploads/jw-player-plugin-for-wordpress/player/player.swf</video:player_loc>\'."\\n".
"\\t\\t".\'<video:requires_subscription>no</video:requires_subscription>\'."\\n";
if ($postCategorys) {
foreach($postCategorys as $postCategory) {
$sitemap .= "\\t\\t".\'<video:category>\'.$postCategory->cat_name.\'</video:category>\'."\\n".
"\\t\\t".\'<video:gallery_loc title="\'.$postCategory->cat_name.\'">\'.$abspath."/".$postCategory->category_nicename.\'</video:gallery_loc>\'."\\n";
}
}
if ($posttags) {
foreach($posttags as $tag) {
$sitemap .= "\\t\\t".\'<video:tag>\'.$tag->name . \' \'.\'</video:tag>\'."\\n";
}
}
$sitemap .="\\t\\t".\'<video:uploader info="http://sitehere.com">blah blah</video:uploader>\'."\\n".
"\\t\\t".\'<video:family_friendly>yes</video:family_friendly>\'."\\n".
"\\t".\'</video:video>\'."\\n".
\'</url>\'."\\n\\n";
}
$sitemap .= \'</urlset>\';
$fp = fopen(ABSPATH . "sitemap.xml", \'w\');
fwrite($fp, $sitemap);
fclose($fp);
}
?>