wordpress自动生成站点地图
在WordPress中,可以使用内置的功能或插件来自动生成站点图和站点地图。以下是使用内置功能的方法:
使用WordPress的内置SEO插件,如Yoast SEO,它提供了生成站点地图的功能。安装插件后,你可以在SEO -> XML Site Maps中找到生成站点地图的选项。
手动添加代码到主题的functions.php文件中,以便在你的网站上生成站点地图。以下是一个简单的例子:
// 注册生成站点地图的函数
function add_xml_sitemap() {
echo '<a href="' . home_url( '/sitemap.xml' ) . '" rel="sitemap">Sitemap</a>';
}
add_action( 'wp_footer', 'add_xml_sitemap' );
// 为站点地图生成XML
function my_sitemap_xml() {
header( 'Content-Type: application/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'post',
'post_status' => 'publish'
) );
foreach( $posts as $post ) {
echo '<url>';
echo '<loc>' . get_permalink( $post->ID ) . '</loc>';
echo '<lastmod>' . date( 'Y-m-d', strtotime( $post->post_modified ) ) . '</lastmod>';
echo '<changefreq>daily</changefreq>';
echo '<priority>0.9</priority>';
echo '</url>';
}
echo '</urlset>';
exit;
}
// 添加站点地图到路由
add_action( 'init', function() {
add_rewrite_rule( 'sitemap.xml$', 'index.php?sitemap=1', 'top' );
add_rewrite_tag( '%sitemap%', '(.?)', 'sitemap=$matches[1]' );
} );
// 处理站点地图请求
add_action( 'template_redirect', function() {
if ( get_query_var( 'sitemap' ) == 1 ) {
my_sitemap_xml();
}
} );
这段代码会在你的网站根目录生成一个sitemap.xml,并且会在页脚添加一个指向站点地图的链接。记得在完成代码添加后,需要激活你的主题,并清空你的浏览器缓存以查看结果。
注意:这个例子提供了一个简单的站点地图生成方法,但你可能需要根据自己的需求进行更多的定制。如果你的网站有大量的自定义post类型或者你希望更详细地控制站点地图的生成,你可能需要编写更复杂的代码来适配这些情况。
方法二
<?php
//wordpress自动生成站点地图(sitemap)纯代码无需插件
// 判断定时计划是否存在
if ( ! wp_next_scheduled( 'sitemap_xml' ) ) {
wp_schedule_event( time(), 'twicedaily', 'sitemap_xml' ); // 每天两次
}
add_action( 'sitemap_xml', 'sitemap_xml_func' );
// 定时计划执行函数
function sitemap_xml_func() {
// 获取文章数量
$count_posts = wp_count_posts();
if ( $count_posts ) {
$published_posts = $count_posts->publish;
$sitemap_num = $published_posts / 5000; // 每个xml文件最多包含5000篇文章
$sitemap_num = ceil($sitemap_num);
// 创建xml文件
for ($i = 1; $i <= $sitemap_num; $i++) {
$postsForSitemap = get_posts(array(
'numberposts' => 3000,
'orderby' => 'modified',
'post_type' => array('post'),
'order' => 'DESC',
'offset' => 3000 * ($i - 1)
));
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($postsForSitemap as $post) {
setup_postdata($post);
$post_url = get_permalink($post->ID);
$post_date = get_the_modified_date( 'c',$post->ID );
$sitemap .= '<url>'.
'<loc>'. $post_url .'</loc>'.
'<lastmod>'. $post_date .'</lastmod>'.
// '<lastmod>'. $postdate[0] .'</lastmod>'.
// '<changefreq>monthly</changefreq>'.
'</url>';
}
$sitemap .= '</urlset>';
$fp = fopen(ABSPATH . "sitemap-".$i.".xml", 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
// 创建sitemap.xml文件
$sitemap_all = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap_all .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
for ($i = 1; $i <= $sitemap_num; $i++) {
$sitemap_all .= '<sitemap>'.
'<loc>'. get_bloginfo('url') .'/sitemap-'.$i.'.xml</loc>'.
'<lastmod>'. date('c') .'</lastmod>'.
'</sitemap>';
}
$sitemap_all .= '</sitemapindex>';
$fp = fopen(ABSPATH . "sitemap.xml", 'w');
fwrite($fp, $sitemap_all);
fclose($fp);
}
}
?>