当前没有人在线
如何为 Asgaros 生成 XML 网站地图?
为 Asgaros 论坛生成一个专门的 XML 网站地图,包含论坛的板块、话题和帖子。我们将通过主题的 functions.php 文件添加一个自定义的网站地图端点,当访问这个端点时,会生成并输出 XML 网站地图。
步骤:
- 在 functions.php 中添加一个重写规则,用于捕获对特定 URL 的请求(例如/forum-sitemap.xml)。
- 添加一个模板重定向,当匹配到该 URL 时,执行我们自定义的网站地图生成函数。
- 在生成函数中,我们查询 Asgaros 论坛的数据(板块、话题、帖子)并按照 XML 网站地图的格式输出。
注意:Asgaros 论坛的数据存储在自定义的数据库表中,我们需要使用自定义查询来获取数据。
假设 Asgaros 论坛的数据库表前缀为
wp_forum_(实际表名可能因前缀而异),我们需要查询以下表:
- 板块(forums)
- 话题(threads)
- 帖子(posts)
另外,我们需要获取 Asgaros 论坛的固定链接结构,以便生成正确的 URL。Asgaros 论坛的固定链接结构通常是在其设置中定义的,但我们可以通过其 API 或直接查询选项来获取。
但是,请注意,Asgaros 论坛的 URL 结构可能因设置而异。为了简化,我们假设您使用的是默认的固定链接结构,即:
论坛首页:通过页面 ID 获取的固定链接
- 板块:/forum/forum-slug/
- 话题:/forum/topic/topic-slug/
- 帖子:/forum/topic/topic-slug/#post-id
由于帖子通常是在话题页面内通过锚点跳转,所以一般不需要单独为每个帖子生成一个单独的 URL,除非您的论坛设置为每个帖子有独立页面(通常不是)。因此,我们通常只为话题和板块生成网站地图。
然而,根据 XML 网站地图的规范,我们只能包含独立的页面,所以通常我们只包含话题页面,而帖子则通过话题页面来索引。
但是,有些 SEO 插件会为每个帖子生成独立的网站地图条目,但 Asgaros 论坛的帖子通常没有独立页面,所以我们不包含单独的帖子。
因此,我们的网站地图将包含:
- 论坛首页
- 所有板块(forums)
- 所有话题(threads)
我们假设您的论坛有大量的板块和话题,所以可能需要分多个网站地图文件。但为了简单起见,我们这里只生成一个网站地图文件。如果数据量很大,建议分多个文件。
由于我们是在 functions.php 中手动生成,所以如果数据量很大,可能会超时或占用过多内存。如果遇到这种情况,您可能需要考虑分批次生成或使用其他方法。
下面是一个基本的实现框架:
注意:请根据您的实际数据库表名和固定链接结构进行调整。
步骤:
- 添加重写规则和查询变量监听。
- 在模板重定向中,如果请求的查询变量匹配,则生成网站地图。
由于我们不需要为每个帖子生成,所以只查询到话题级别。
代码示例:
首先,添加重写规则和查询变量。
您可以通过修改主题的
functions.php文件为 Asgaros 论坛生成专门的 XML 网站地图。以下是完整的实现方案:<?php
// 为 Asgaros 论坛生成专用网站地图
function generate_asgaros_sitemap() {
// 检查是否是网站地图请求
if (isset($_GET['asgaros-sitemap']) && $_GET['asgaros-sitemap'] === 'xml') {
header('Content-Type: text/xml; charset=utf-8');
header('X-Robots-Tag: noindex, follow', true);global $wpdb, $asgarosforum;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<?xml-stylesheet type="text/xsl" href="' . esc_url(get_stylesheet_directory_uri() . '/sitemap.xsl') . '"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';// 获取论坛主页面
$forum_page_id = get_option('asgarosforum_main_page_id');
if ($forum_page_id) {
$forum_url = get_permalink($forum_page_id);
echo '<url>';
echo '<loc>' . esc_url($forum_url) . '</loc>';
echo '<changefreq>daily</changefreq>';
echo '<priority>1.0</priority>';
echo '</url>';
}// 获取所有论坛板块
if (class_exists('AsgarosForum') && isset($asgarosforum->db)) {
$forums = $wpdb->get_results(
"SELECT f.id, f.name, f.slug, f.description,
COUNT(DISTINCT t.id) as thread_count,
MAX(p.date) as last_modified
FROM {$asgarosforum->tables->forums} f
LEFT JOIN {$asgarosforum->tables->threads} t ON f.id = t.parent_id
LEFT JOIN {$asgarosforum->tables->posts} p ON t.id = p.parent_id
WHERE f.parent_forum = 0 AND f.approved = 1
GROUP BY f.id
ORDER BY f.sort, f.name"
);foreach ($forums as $forum) {
$forum_link = $asgarosforum->get_link('forum', $forum->id);
echo '<url>';
echo '<loc>' . esc_url($forum_link) . '</loc>';
echo '<lastmod>' . mysql2date('c', $forum->last_modified) . '</lastmod>';
echo '<changefreq>' . ($forum->thread_count > 100 ? 'hourly' : 'daily') . '</changefreq>';
echo '<priority>0.8</priority>';
if (!empty($forum->description)) {
echo '<image:image>';
echo '<image:title>' . esc_html($forum->name) . '</image:title>';
echo '<image:caption>' . esc_html(wp_strip_all_tags($forum->description)) . '</image:caption>';
echo '</image:image>';
}
echo '</url>';// 获取该板块下的所有主题
$topics = $wpdb->get_results($wpdb->prepare(
"SELECT t.id, t.name, t.slug, t.status, t.sticky, t.closed,
p.date as last_post_date,
(SELECT COUNT(*) FROM {$asgarosforum->tables->posts} p2
WHERE p2.parent_id = t.id) as post_count
FROM {$asgarosforum->tables->threads} t
LEFT JOIN {$asgarosforum->tables->posts} p ON t.id = p.parent_id
WHERE t.parent_id = %d AND t.approved = 1
GROUP BY t.id
ORDER BY t.sticky DESC, t.closed ASC, p.date DESC
LIMIT 1000", // 限制数量避免内存问题
$forum->id
));foreach ($topics as $topic) {
if ($topic->status == 0) { // 只包含正常主题
$topic_link = $asgarosforum->get_link('topic', $topic->id);
echo '<url>';
echo '<loc>' . esc_url($topic_link) . '</loc>';
echo '<lastmod>' . mysql2date('c', $topic->last_post_date) . '</lastmod>';// 根据主题活跃度设置更新频率
$changefreq = 'weekly';
if ($topic->post_count > 50) {
$changefreq = 'daily';
} elseif ($topic->post_count > 10) {
$changefreq = 'weekly';
}
echo '<changefreq>' . $changefreq . '</changefreq>';// 根据置顶/热门程度设置优先级
$priority = 0.6;
if ($topic->sticky == 1) $priority = 0.8;
if ($topic->post_count > 100) $priority = 0.7;echo '<priority>' . $priority . '</priority>';
echo '</url>';
}
}
}
}echo '</urlset>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap');// 添加网站地图索引支持
function generate_asgaros_sitemap_index() {
if (isset($_GET['asgaros-sitemap-index']) && $_GET['asgaros-sitemap-index'] === 'xml') {
header('Content-Type: text/xml; charset=utf-8');echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';$sitemap_url = home_url('/?asgaros-sitemap=xml');
echo '<sitemap>';
echo '<loc>' . esc_url($sitemap_url) . '</loc>';
echo '<lastmod>' . current_time('c') . '</lastmod>';
echo '</sitemap>';echo '</sitemapindex>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap_index');
?>
为 Asgaros 论坛生成一个专门的 XML 网站地图,包含论坛的板块、话题和帖子。我们将通过主题的 functions.php 文件添加一个自定义的网站地图端点,当访问这个端点时,会生成并输出 XML 网站地图。
步骤:
- 在 functions.php 中添加一个重写规则,用于捕获对特定 URL 的请求(例如/forum-sitemap.xml)。
- 添加一个模板重定向,当匹配到该 URL 时,执行我们自定义的网站地图生成函数。
- 在生成函数中,我们查询 Asgaros 论坛的数据(板块、话题、帖子)并按照 XML 网站地图的格式输出。
注意:Asgaros 论坛的数据存储在自定义的数据库表中,我们需要使用自定义查询来获取数据。
假设 Asgaros 论坛的数据库表前缀为wp_forum_(实际表名可能因前缀而异),我们需要查询以下表:
- 板块(forums)
- 话题(threads)
- 帖子(posts)
另外,我们需要获取 Asgaros 论坛的固定链接结构,以便生成正确的 URL。Asgaros 论坛的固定链接结构通常是在其设置中定义的,但我们可以通过其 API 或直接查询选项来获取。
但是,请注意,Asgaros 论坛的 URL 结构可能因设置而异。为了简化,我们假设您使用的是默认的固定链接结构,即:
论坛首页:通过页面 ID 获取的固定链接
- 板块:/forum/forum-slug/
- 话题:/forum/topic/topic-slug/
- 帖子:/forum/topic/topic-slug/#post-id
由于帖子通常是在话题页面内通过锚点跳转,所以一般不需要单独为每个帖子生成一个单独的 URL,除非您的论坛设置为每个帖子有独立页面(通常不是)。因此,我们通常只为话题和板块生成网站地图。
然而,根据 XML 网站地图的规范,我们只能包含独立的页面,所以通常我们只包含话题页面,而帖子则通过话题页面来索引。
但是,有些 SEO 插件会为每个帖子生成独立的网站地图条目,但 Asgaros 论坛的帖子通常没有独立页面,所以我们不包含单独的帖子。
因此,我们的网站地图将包含:
- 论坛首页
- 所有板块(forums)
- 所有话题(threads)
我们假设您的论坛有大量的板块和话题,所以可能需要分多个网站地图文件。但为了简单起见,我们这里只生成一个网站地图文件。如果数据量很大,建议分多个文件。
由于我们是在 functions.php 中手动生成,所以如果数据量很大,可能会超时或占用过多内存。如果遇到这种情况,您可能需要考虑分批次生成或使用其他方法。
下面是一个基本的实现框架:
注意:请根据您的实际数据库表名和固定链接结构进行调整。
步骤:
- 添加重写规则和查询变量监听。
- 在模板重定向中,如果请求的查询变量匹配,则生成网站地图。
由于我们不需要为每个帖子生成,所以只查询到话题级别。
代码示例:
首先,添加重写规则和查询变量。
您可以通过修改主题的 functions.php文件为 Asgaros 论坛生成专门的 XML 网站地图。以下是完整的实现方案:
<?php
// 为 Asgaros 论坛生成专用网站地图
function generate_asgaros_sitemap() {
// 检查是否是网站地图请求
if (isset($_GET['asgaros-sitemap']) && $_GET['asgaros-sitemap'] === 'xml') {
header('Content-Type: text/xml; charset=utf-8');
header('X-Robots-Tag: noindex, follow', true);global $wpdb, $asgarosforum;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<?xml-stylesheet type="text/xsl" href="' . esc_url(get_stylesheet_directory_uri() . '/sitemap.xsl') . '"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';// 获取论坛主页面
$forum_page_id = get_option('asgarosforum_main_page_id');
if ($forum_page_id) {
$forum_url = get_permalink($forum_page_id);
echo '<url>';
echo '<loc>' . esc_url($forum_url) . '</loc>';
echo '<changefreq>daily</changefreq>';
echo '<priority>1.0</priority>';
echo '</url>';
}// 获取所有论坛板块
if (class_exists('AsgarosForum') && isset($asgarosforum->db)) {
$forums = $wpdb->get_results(
"SELECT f.id, f.name, f.slug, f.description,
COUNT(DISTINCT t.id) as thread_count,
MAX(p.date) as last_modified
FROM {$asgarosforum->tables->forums} f
LEFT JOIN {$asgarosforum->tables->threads} t ON f.id = t.parent_id
LEFT JOIN {$asgarosforum->tables->posts} p ON t.id = p.parent_id
WHERE f.parent_forum = 0 AND f.approved = 1
GROUP BY f.id
ORDER BY f.sort, f.name"
);foreach ($forums as $forum) {
$forum_link = $asgarosforum->get_link('forum', $forum->id);
echo '<url>';
echo '<loc>' . esc_url($forum_link) . '</loc>';
echo '<lastmod>' . mysql2date('c', $forum->last_modified) . '</lastmod>';
echo '<changefreq>' . ($forum->thread_count > 100 ? 'hourly' : 'daily') . '</changefreq>';
echo '<priority>0.8</priority>';
if (!empty($forum->description)) {
echo '<image:image>';
echo '<image:title>' . esc_html($forum->name) . '</image:title>';
echo '<image:caption>' . esc_html(wp_strip_all_tags($forum->description)) . '</image:caption>';
echo '</image:image>';
}
echo '</url>';// 获取该板块下的所有主题
$topics = $wpdb->get_results($wpdb->prepare(
"SELECT t.id, t.name, t.slug, t.status, t.sticky, t.closed,
p.date as last_post_date,
(SELECT COUNT(*) FROM {$asgarosforum->tables->posts} p2
WHERE p2.parent_id = t.id) as post_count
FROM {$asgarosforum->tables->threads} t
LEFT JOIN {$asgarosforum->tables->posts} p ON t.id = p.parent_id
WHERE t.parent_id = %d AND t.approved = 1
GROUP BY t.id
ORDER BY t.sticky DESC, t.closed ASC, p.date DESC
LIMIT 1000", // 限制数量避免内存问题
$forum->id
));foreach ($topics as $topic) {
if ($topic->status == 0) { // 只包含正常主题
$topic_link = $asgarosforum->get_link('topic', $topic->id);
echo '<url>';
echo '<loc>' . esc_url($topic_link) . '</loc>';
echo '<lastmod>' . mysql2date('c', $topic->last_post_date) . '</lastmod>';// 根据主题活跃度设置更新频率
$changefreq = 'weekly';
if ($topic->post_count > 50) {
$changefreq = 'daily';
} elseif ($topic->post_count > 10) {
$changefreq = 'weekly';
}
echo '<changefreq>' . $changefreq . '</changefreq>';// 根据置顶/热门程度设置优先级
$priority = 0.6;
if ($topic->sticky == 1) $priority = 0.8;
if ($topic->post_count > 100) $priority = 0.7;echo '<priority>' . $priority . '</priority>';
echo '</url>';
}
}
}
}echo '</urlset>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap');// 添加网站地图索引支持
function generate_asgaros_sitemap_index() {
if (isset($_GET['asgaros-sitemap-index']) && $_GET['asgaros-sitemap-index'] === 'xml') {
header('Content-Type: text/xml; charset=utf-8');echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';$sitemap_url = home_url('/?asgaros-sitemap=xml');
echo '<sitemap>';
echo '<loc>' . esc_url($sitemap_url) . '</loc>';
echo '<lastmod>' . current_time('c') . '</lastmod>';
echo '</sitemap>';echo '</sitemapindex>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap_index');
?>
如果您的 Asgaros 论坛有大量主题,可以使用分页版本:
<?php
// 分页的 Asgaros 网站地图
function generate_asgaros_sitemap_paged() {
if (isset($_GET['asgaros-sitemap']) && is_numeric($_GET['asgaros-sitemap'])) {
$page = intval($_GET['asgaros-sitemap']);
$per_page = 500; // 每页URL数量header('Content-Type: text/xml; charset=utf-8');
global $wpdb, $asgarosforum;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';$offset = ($page - 1) * $per_page;
if ($page == 1) {
// 第一页包含论坛首页和板块
$forum_page_id = get_option('asgarosforum_main_page_id');
if ($forum_page_id) {
echo '<url>';
echo '<loc>' . esc_url(get_permalink($forum_page_id)) . '</loc>';
echo '<priority>1.0</priority>';
echo '</url>';
}// 添加所有板块
$forums = $wpdb->get_results(
"SELECT id, slug FROM {$asgarosforum->tables->forums}
WHERE parent_forum = 0 AND approved = 1
ORDER BY sort, name"
);foreach ($forums as $forum) {
echo '<url>';
echo '<loc>' . esc_url($asgarosforum->get_link('forum', $forum->id)) . '</loc>';
echo '<priority>0.8</priority>';
echo '</url>';
}
}// 添加主题(分页)
$topics = $wpdb->get_results($wpdb->prepare(
"SELECT t.id, t.name, t.slug, t.parent_id,
MAX(p.date) as last_modified
FROM {$asgarosforum->tables->threads} t
LEFT JOIN {$asgarosforum->tables->posts} p ON t.id = p.parent_id
WHERE t.approved = 1 AND t.status = 0
GROUP BY t.id
ORDER BY t.id DESC
LIMIT %d OFFSET %d",
$per_page, $offset
));foreach ($topics as $topic) {
$topic_link = $asgarosforum->get_link('topic', $topic->id);
echo '<url>';
echo '<loc>' . esc_url($topic_link) . '</loc>';
echo '<lastmod>' . mysql2date('c', $topic->last_modified) . '</lastmod>';
echo '<priority>0.6</priority>';
echo '</url>';
}echo '</urlset>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap_paged');// 生成分页网站地图索引
function generate_asgaros_sitemap_paged_index() {
if (isset($_GET['asgaros-sitemaps']) && $_GET['asgaros-sitemaps'] === 'index') {
global $wpdb, $asgarosforum;header('Content-Type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';// 计算总主题数
$total_topics = $wpdb->get_var("SELECT COUNT(*) FROM {$asgarosforum->tables->threads} WHERE approved = 1 AND status = 0");
$total_forums = $wpdb->get_var("SELECT COUNT(*) FROM {$asgarosforum->tables->forums} WHERE parent_forum = 0 AND approved = 1");$per_page = 500;
$total_pages = ceil(($total_topics + $total_forums + 1) / $per_page);for ($i = 1; $i <= $total_pages; $i++) {
echo '<sitemap>';
echo '<loc>' . esc_url(home_url("/?asgaros-sitemap={$i}")) . '</loc>';
echo '<lastmod>' . current_time('c') . '</lastmod>';
echo '</sitemap>';
}echo '</sitemapindex>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap_paged_index');
?>
如果您的 Asgaros 论坛有大量主题,可以使用分页版本:
<?php
// 分页的 Asgaros 网站地图
function generate_asgaros_sitemap_paged() {
if (isset($_GET['asgaros-sitemap']) && is_numeric($_GET['asgaros-sitemap'])) {
$page = intval($_GET['asgaros-sitemap']);
$per_page = 500; // 每页URL数量header('Content-Type: text/xml; charset=utf-8');
global $wpdb, $asgarosforum;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';$offset = ($page - 1) * $per_page;
if ($page == 1) {
// 第一页包含论坛首页和板块
$forum_page_id = get_option('asgarosforum_main_page_id');
if ($forum_page_id) {
echo '<url>';
echo '<loc>' . esc_url(get_permalink($forum_page_id)) . '</loc>';
echo '<priority>1.0</priority>';
echo '</url>';
}// 添加所有板块
$forums = $wpdb->get_results(
"SELECT id, slug FROM {$asgarosforum->tables->forums}
WHERE parent_forum = 0 AND approved = 1
ORDER BY sort, name"
);foreach ($forums as $forum) {
echo '<url>';
echo '<loc>' . esc_url($asgarosforum->get_link('forum', $forum->id)) . '</loc>';
echo '<priority>0.8</priority>';
echo '</url>';
}
}// 添加主题(分页)
$topics = $wpdb->get_results($wpdb->prepare(
"SELECT t.id, t.name, t.slug, t.parent_id,
MAX(p.date) as last_modified
FROM {$asgarosforum->tables->threads} t
LEFT JOIN {$asgarosforum->tables->posts} p ON t.id = p.parent_id
WHERE t.approved = 1 AND t.status = 0
GROUP BY t.id
ORDER BY t.id DESC
LIMIT %d OFFSET %d",
$per_page, $offset
));foreach ($topics as $topic) {
$topic_link = $asgarosforum->get_link('topic', $topic->id);
echo '<url>';
echo '<loc>' . esc_url($topic_link) . '</loc>';
echo '<lastmod>' . mysql2date('c', $topic->last_modified) . '</lastmod>';
echo '<priority>0.6</priority>';
echo '</url>';
}echo '</urlset>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap_paged');// 生成分页网站地图索引
function generate_asgaros_sitemap_paged_index() {
if (isset($_GET['asgaros-sitemaps']) && $_GET['asgaros-sitemaps'] === 'index') {
global $wpdb, $asgarosforum;header('Content-Type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';// 计算总主题数
$total_topics = $wpdb->get_var("SELECT COUNT(*) FROM {$asgarosforum->tables->threads} WHERE approved = 1 AND status = 0");
$total_forums = $wpdb->get_var("SELECT COUNT(*) FROM {$asgarosforum->tables->forums} WHERE parent_forum = 0 AND approved = 1");$per_page = 500;
$total_pages = ceil(($total_topics + $total_forums + 1) / $per_page);for ($i = 1; $i <= $total_pages; $i++) {
echo '<sitemap>';
echo '<loc>' . esc_url(home_url("/?asgaros-sitemap={$i}")) . '</loc>';
echo '<lastmod>' . current_time('c') . '</lastmod>';
echo '</sitemap>';
}echo '</sitemapindex>';
exit;
}
}
add_action('init', 'generate_asgaros_sitemap_paged_index');
?>
自动通知搜索引擎
在
functions.php中添加自动 ping 功能:<?php
// 当有新主题/回复时通知搜索引擎
function asgaros_notify_search_engines($post_id, $post) {
if (!wp_is_post_revision($post_id) && $post->post_type == 'forum') {
$sitemap_url = home_url('/?asgaros-sitemap=xml');// Ping Google
wp_remote_get('http://www.google.com/ping?sitemap=' . urlencode($sitemap_url));// Ping Bing
wp_remote_get('http://www.bing.com/ping?sitemap=' . urlencode($sitemap_url));
}
}
add_action('save_post', 'asgaros_notify_search_engines', 10, 2);// 在 robots.txt 中添加网站地图
function asgaros_add_sitemap_to_robots($output) {
$sitemap_url = home_url('/?asgaros-sitemap=xml');
$output .= "\nSitemap: {$sitemap_url}\n";
return $output;
}
add_filter('robots_txt', 'asgaros_add_sitemap_to_robots', 10, 1);// 添加网站地图链接到 WordPress 默认网站地图
function asgaros_add_to_wp_sitemap($sitemaps) {
$sitemaps['asgaros'] = array(
'name' => 'forum',
'posts' => array(),
);
return $sitemaps;
}
add_filter('wp_sitemaps_add_provider', 'asgaros_add_to_wp_sitemap', 10, 2);
?>
自动通知搜索引擎
在 functions.php中添加自动 ping 功能:
<?php
// 当有新主题/回复时通知搜索引擎
function asgaros_notify_search_engines($post_id, $post) {
if (!wp_is_post_revision($post_id) && $post->post_type == 'forum') {
$sitemap_url = home_url('/?asgaros-sitemap=xml');// Ping Google
wp_remote_get('http://www.google.com/ping?sitemap=' . urlencode($sitemap_url));// Ping Bing
wp_remote_get('http://www.bing.com/ping?sitemap=' . urlencode($sitemap_url));
}
}
add_action('save_post', 'asgaros_notify_search_engines', 10, 2);// 在 robots.txt 中添加网站地图
function asgaros_add_sitemap_to_robots($output) {
$sitemap_url = home_url('/?asgaros-sitemap=xml');
$output .= "\nSitemap: {$sitemap_url}\n";
return $output;
}
add_filter('robots_txt', 'asgaros_add_sitemap_to_robots', 10, 1);// 添加网站地图链接到 WordPress 默认网站地图
function asgaros_add_to_wp_sitemap($sitemaps) {
$sitemaps['asgaros'] = array(
'name' => 'forum',
'posts' => array(),
);
return $sitemaps;
}
add_filter('wp_sitemaps_add_provider', 'asgaros_add_to_wp_sitemap', 10, 2);
?>
将代码添加到主题的
functions.php文件底部如果需要 XSL 样式,创建
sitemap.xsl文件到主题目录访问
https://yoursite.com/?asgaros-sitemap=xml查看网站地图将网站地图提交到 Google Search Console 和 Bing Webmaster Tools
在主题目录中创建
sitemap.xsl文件:<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/><xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Asgaros Forum Sitemap</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body { font-family: Helvetica, Arial, sans-serif; font-size: 13px; }
#content { margin: 0 auto; width: 1000px; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; padding: 5px; background-color: #2d609b; color: #fff; }
td { padding: 5px; border: 1px solid #ccc; }
tr:nth-child(even) { background-color: #f0f0f0; }
.center { text-align: center; }
.header { background-color: #f1f1f1; padding: 20px; margin-bottom: 20px; }
</style>
</head>
<body>
<div id="content">
<div class="header">
<h1>Asgaros Forum XML Sitemap</h1>
<p>Generated on <xsl:value-of select="current-dateTime()"/></p>
</div>
<table>
<tr>
<th>URL</th>
<th width="150">Last Modified</th>
<th width="100">Change Frequency</th>
<th width="80">Priority</th>
</tr>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<tr>
<td><a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc"/></a></td>
<td><xsl:value-of select="sitemap:lastmod"/></td>
<td><xsl:value-of select="sitemap:changefreq"/></td>
<td><xsl:value-of select="sitemap:priority"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
-
将代码添加到主题的
functions.php文件底部 -
如果需要 XSL 样式,创建
sitemap.xsl文件到主题目录 -
访问
https://yoursite.com/?asgaros-sitemap=xml查看网站地图 -
将网站地图提交到 Google Search Console 和 Bing Webmaster Tools
在主题目录中创建 sitemap.xsl文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/><xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Asgaros Forum Sitemap</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body { font-family: Helvetica, Arial, sans-serif; font-size: 13px; }
#content { margin: 0 auto; width: 1000px; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; padding: 5px; background-color: #2d609b; color: #fff; }
td { padding: 5px; border: 1px solid #ccc; }
tr:nth-child(even) { background-color: #f0f0f0; }
.center { text-align: center; }
.header { background-color: #f1f1f1; padding: 20px; margin-bottom: 20px; }
</style>
</head>
<body>
<div id="content">
<div class="header">
<h1>Asgaros Forum XML Sitemap</h1>
<p>Generated on <xsl:value-of select="current-dateTime()"/></p>
</div>
<table>
<tr>
<th>URL</th>
<th width="150">Last Modified</th>
<th width="100">Change Frequency</th>
<th width="80">Priority</th>
</tr>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<tr>
<td><a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc"/></a></td>
<td><xsl:value-of select="sitemap:lastmod"/></td>
<td><xsl:value-of select="sitemap:changefreq"/></td>
<td><xsl:value-of select="sitemap:priority"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
