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.

Most Viral Tool - SEO Audit Tool  | Reseller Profit Tracker Generator | Freelance Invoice Generator | ADHD Planner Generator

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.

Trending Today- Earn $$$ FREE  | Trending LIFE Quotes | HOT DEBATES | Autograph | FREE PAID Tools | Advertise FREE |

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=”https://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
//https://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
//https://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”;

//https://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.

//https://us3.php.net/while ; https://codex.wordpress.org/The_Loop ; https://codex.wordpress.org/The_Loop_in_Action
//https://codex.wordpress.org/Function_Reference/the_time ; https://codex.wordpress.org/Function_Reference/the_permalink
//https://codex.wordpress.org/Function_Reference/the_title ; https://codex.wordpress.org/Function_Reference/comments_number
while(have_posts()): the_post();
//https://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
//https://codex.wordpress.org/Function_Reference/wp_reset_query
wp_reset_query();
?>
<h2>Archives:</h2>
<ul class=”sitemap-archives”>
<?php
//https://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

How useful was this post?

Click on a star to rate it!