WordPress Dynamic XML Sitemap Without Plugin

WordPress Dynamic XML Sitemap Without Plugin

After logging into WordPress dashboard go to Appearance > Editor from the menu at the left hand side.

Now locate the functions.php file

The location to the file may look like : /home/username/public_html/wp-content/themes/currently_active_theme/functions.php

Make a local backup of this file in case anything goes wrong and you may need to revert to previous stable code.

click on it to begin editing it.

Put this below code in your functions.php to to Add WordPress XML Sitemap Without Plugin

 

add_action(“publish_post”, “eg_create_sitemap”);
add_action(“publish_page”, “eg_create_sitemap”);
function eg_create_sitemap() {
$postsForSitemap = get_posts(array(
‘numberposts’ => -1,
‘orderby’ => ‘modified’,
‘post_type’ => array(‘post’,’page’),
‘order’ => ‘DESC’
));
$sitemap = ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
$sitemap .= ‘<?xml-stylesheet type=”text/xsl” href=”sitemap-style.xsl”?>’;
$sitemap .= ‘<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″>’;
foreach($postsForSitemap as $post) {
setup_postdata($post);
$postdate = explode(” “, $post->post_modified);
$sitemap .= ‘<url>’.
‘<loc>’. get_permalink($post->ID) .'</loc>’.
‘<priority>1</priority>’.
‘<lastmod>’. $postdate[0] .'</lastmod>’.
‘<changefreq>daily</changefreq>’.
‘</url>’;
}
$sitemap .= ‘</urlset>’;
$fp = fopen(ABSPATH . “sitemap.xml”, ‘w’);
fwrite($fp, $sitemap);
fclose($fp);
}

 

Next copy existing template  as  /home/username/public_html/wp-content/themes/currently_active_theme/

so for us our path is /SHAREASKCOM_1/wp-content/themes/share-askchild

Next name copied file as sitemap here  /home/username/public_html/wp-content/themes/currently_active_theme/sitemap

so for us our path is /SHAREASKCOM_1/wp-content/themes/share-askchild/sitemap

Now find </header> in the new file sitemap and paste below code after </header>

 
<div class=”html-sitemap”>

<h2>Pages:</h2>
<ul class=”sitemap-pages”>
<?php
//http://codex.wordpress.org/Function_Reference/wp_list_pages
wp_list_pages(‘exclude=889&title_li=’); //***Exclude page Id, separated by comma. I excluded the sitemap of this blog (page_ID=889).
?>
</ul>

<h2>Posts:</h2>
<ul>
<?php
//http://codex.wordpress.org/Function_Reference/get_categories
$cats = get_categories(‘exclude=’); //***Exclude categories by ID, separated by comma if you like.

foreach ($cats as $cat) {
echo ‘<li class=”category”>’.”\n”.'<h3><span class=”grey”>Category: </span>’.$cat->cat_name.'</h3>’.”\n”;
echo ‘<ul class=”cat-posts”>’.”\n”;

//http://codex.wordpress.org/Function_Reference/query_posts
query_posts(‘posts_per_page=-1&cat=’.$cat->cat_ID); //-1 shows all posts per category. 1 to show most recent post.

//http://us3.php.net/while ; http://codex.wordpress.org/The_Loop ; http://codex.wordpress.org/The_Loop_in_Action
//http://codex.wordpress.org/Function_Reference/the_time ; http://codex.wordpress.org/Function_Reference/the_permalink
//http://codex.wordpress.org/Function_Reference/the_title ; http://codex.wordpress.org/Function_Reference/comments_number
while(have_posts()): the_post();
//http://codex.wordpress.org/Function_Reference/get_the_category
$category = get_the_category();
//Display a post once, even if it is in multiple categories/subcategories. Lists the post in the first Category displayed.
if ($category[0]->cat_ID == $cat->cat_ID) {?>
<li><?php the_time(‘M d, Y’)?> &raquo; <a href=”<?php the_permalink() ?>” title=”Permanent Link to: <?php the_title(); ?>”>
<?php the_title(); ?></a> (<?php comments_number(‘0’, ‘1’, ‘%’); ?>)</li>
<?php } //endif
endwhile; //endwhile
?>
</ul>
</li>
<?php } ?>
</ul>
<?php
//http://codex.wordpress.org/Function_Reference/wp_reset_query
wp_reset_query();
?>
<h2>Archives:</h2>
<ul class=”sitemap-archives”>
<?php
//http://codex.wordpress.org/Function_Reference/wp_get_archives
wp_get_archives(‘type=monthly&show_post_count=true’);
?>
</ul>
</div>

 

This will give us WordPress XML Sitemap Without Plugin

Let me know besumitbe@gmail.com/skype-bpshbp if you have any Query