天天看點

php網站地圖代碼,純代碼添加網站地圖

效果圖

詳細步驟

1.在網站根目錄下建立一個php檔案 例如:sitemap.php

2.在sitemap.php檔案内添加如下代碼

require('./wp-blog-header.php');

header("Content-type: text/xml");

header('HTTP/1.1 200 OK');

$posts_to_show = 1000;

echo '<?xml version="1.0" encoding="UTF-8"?>';

echo ''

?>

daily

1.0

$myposts = get_posts( "numberposts=" . $posts_to_show );

foreach( $myposts as $post ) { ?>

monthly

0.6

$mypages = get_pages();

if(count($mypages) > 0) {

foreach($mypages as $page) { ?>

<?php echo get_page_link($page->ID); ?>

<?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00

weekly

0.6

$terms = get_terms('category', 'orderby=name&hide_empty=0' );

$count = count($terms);

if($count > 0){

foreach ($terms as $term) { ?>

<?php echo get_term_link($term, $term->slug); ?>

weekly

0.8

$tags = get_terms("post_tag");

foreach ( $tags as $key => $tag ) {

$link = get_term_link( intval($tag->term_id), "post_tag" );

if ( is_wp_error( $link ) )

return false;

$tags[ $key ]->link = $link;

?>

monthly

0.4

php網站地圖代碼,純代碼添加網站地圖

3.儲存就可以通過(https://你的域名/sitemap.php)通路