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) {
$postdate = explode(” “, $post->post_modified);
$sitemap .= ‘<url>’.
‘<loc>’. get_permalink($post->ID) .'</loc>’.
‘<lastmod>’. $postdate[0] .'</lastmod>’.
$sitemap .= ‘</urlset>’;
$fp = fopen(ABSPATH . “sitemap.xml”, ‘w’);
fwrite($fp, $sitemap);


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”>

<ul class=”sitemap-pages”>
wp_list_pages(‘exclude=889&title_li=’); //***Exclude page Id, separated by comma. I excluded the sitemap of this blog (page_ID=889).

$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”;

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();
$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
<?php } ?>
<ul class=”sitemap-archives”>


This will give us WordPress XML Sitemap Without Plugin

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