wordpress纯代码如何生成xml网站地图

wor

wordpress纯代码如何生成xml网站地图

dpress纯代码如何生成网站地图?想必大家在做wordpress网站地图的时候,不想使用插件,而想用纯代码的方式实现,下面木卢就直接贴上代码,仅供大家参考:
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$args = array( 'numberposts' => 1000, 'offset' =>0,'order' => 'ASC');
echo '<?xml version="1.0"encoding="UTF-8"?>';
echo'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> -->
<url>
<loc>http://seo.muluseo.cn</loc>
<lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 文章页面 */
$myposts = get_posts($args);
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } /* 文章循环结束 */ ?>
</urlset>
上面代码中核心函数我们用到了wordpress内置函数get_posts(),通过get_posts()函数可以设置文章的指定字段、类型或是顺序等条件。且文章以数组的形式返回。我们在代码头部已经定义了变量$args = array( 'numberposts' => 1000, 'offset' =>0,'order' => 'ASC');这里面就有三个重要参数, 'numberposts' 指定文章调用的数量,'offset' 指定文章的起始ID,'order' 表示按升序输出。下面是get_posts()的所有变量$args的参数。
<?php
$args = array(
//显示的文章数量
'numberposts' => 10,
//以第几篇文章为起始位置
'offset' => 0,
//调用分类的ID,多个用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。 
'category' => ,
//文章排序规则
'orderby' => 'post_date',
//升序、降序 'ASC' —— 升序 (低到高) 'DESC' —— 降序 (高到底)
'order' => 'DESC',
//要显示文章的ID
'include' => ,
//要排除文章的ID
'exclude' => ,
//自定义字段名称
'meta_key' => ,
//自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。
'meta_value' => ,
//post(日志)——默认,page(页面),attachment(附件),any —— (所有)
'post_type' => 'post',
//文章的 mime 类型
'post_mime_type' => ,
//要显示文章的父级 ID
'post_parent' => ,
//文章状态
'post_status' => 'publish' );
?>
好了,今天木卢就分享到这里,希望对你有用。
留言与评论(共有 0 条评论)
   
验证码: