wordpress自动生成站点地图sitemap.xml教程
分类: 主题优化、站点信息、经验总结 1781 4
1. 安装依赖
yarn add @nuxtjs/sitemap axios
或者
npm i @nuxtjs/sitemap
npm install axios
2. 在配置文件nuxt.config.js添加生成代码
2.1 在最外层(export default {
上面)添加代码:
const axios = require('axios');
2.2 在module里边添加依赖(原有的别删除,注意除了最后一项外前面每一行依赖后面有逗号):
modules: [
'@nuxtjs/axios',
'@nuxtjs/sitemap'
],
3. 添加生成代码:
sitemap: {
path: '/sitemap.xml',
hostname: 'https://www.caidhome.cn/', // 网址
cacheTime: 1000 * 60 * 60 * 24,
gzip: true,
generate: false,
exclude: [
'/404'
],
routes (callback) {
axios.all([
axios.get('https://www.caidhome.cn/wp-json/xm-blog/v1/menu'),
axios.get('https://www.caidhome.cn/wp-json/wp/v2/posts', {
params: {
page: 1,
per_page: 100,
_embed: true
},
data: { progress: false }
}),
axios.get('https://www.caidhome.cn/wp-json/xm-blog/v1/info')
]).then(axios.spread(function (menu, posts, info) {
let now = new Date();
now.setHours(now.getHours(), now.getMinutes() - now.getTimezoneOffset());
let indexRoutes = [
{
url: '/',
changefreq: 'daily',
priority: 1,
lastmodISO: now.toISOString()
}
]
let menuRoutes = menu.data.mainMenu.map((data) => {
let url = ''
if (data.object === 'category') {
url = '/category/1?type=' + data.object_id + '&title=' + data.title
}
if (data.object === 'page') {
url = '/page/' + data.object_id
}
if (data.object === 'post_tag') {
url = '/tags/1?type=' + data.term_id + '&title=' + data.name
}
if (data.object === 'custom') {
url = data.url
}
return {
url: url,
changefreq: 'monthly',
priority: 0.8,
lastmodISO: data.post_modified
}
});
let postsRoutes = posts.data.map((data) => {
return {
url: '/' + data.id,
changefreq: 'daily',
priority: 0.9,
lastmodISO: data.modified
}
});
let tagsRoutes = info.data.tagCloud.map((data) => {
return {
url: `/tags/1?type=${data.term_id}&title=${data.name}`,
changefreq: 'weekly',
priority: 0.7,
lastmodISO: now
}
})
callback(null, indexRoutes.concat(menuRoutes, postsRoutes, tagsRoutes));
}), function (err) {
throw (err);
});
}
}
4. 测试
访问:'域名+/sitemap.xml',发现已经有XML格式的sitemap文件了,这样就可以在百度推送里添加链接,进行抓取推送了。
这是我的sitemap.xml结果:
参考链接:
会coding的HAM | BG7ZAG(友链)
共 4 条评论关于 “wordpress自动生成站点地图sitemap.xml教程”