LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Tips » How To Create A WordPress Categories XML Sitemap Without Plugin?

How To Create A WordPress Categories XML Sitemap Without Plugin?

How-To-Create-A-WordPress-Categories-XML-Sitemap-Without-Plugin
Sitemap are extremely important for SEO of one’s Blog or Web page. Sitemap mentioned the SE make use of the mentioned URLs in there results. In short sitemap is the first task toward SEO. Now a days web-masters first generate sitemap instead of writing some content. So now it’s also wise to use sitemaps on your blog to be in the SE results and acquire organic traffic. < ! --more-->

Inside WordPress, there are many plugins which can be generation your sitemap automatically when you publish a web page or post however you better know that plugins could cause a heavy load on the hosting and also make your blog unsecure. So its time to leave using plugins for sitemap in WordPress as we also hate WordPress plugins.

So here below we have some codes that will generate your WordPress categories sitemap not posts and pages sitemap with stylesheet whenever you will publish a post or a page or update the with a category then it will be automatically updated. It will generate a real XML file in your hosting not a fake one as plugins do.

How To Make WordPress Categories Sitemap Without Any Plugin?

Step 1:) Copy the below code and Paste in in your Theme Function(function.php) file.

/* ------------------------------------------------------------------------- *
* WordPress Categories Dynamic XML Sitemap Without Plugin
* Codes By Emrah Gunduz & All In One SEO
* Updated And Edited By EXEIdeas
/* ------------------------------------------------------------------------- */
add_action("publish_post", "cat_create_sitemap");
add_action("publish_page", "cat_create_sitemap");
function cat_create_sitemap() {
 $categoriesForSitemap = get_categories(array(
 'hide_empty' => 0, 
 'depth' => 0, 
 'hierarchical' => false
 ));
 $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($categoriesForSitemap as $category) {
 $sitemap .= '<url>'.
 '<loc>'. get_category_link($category->term_id) .'</loc>'.
 '<priority>1</priority>'.
 '<lastmod>2015-02-01</lastmod>'.
 '<changefreq>daily</changefreq>'.
 '</url>';
 }
 $sitemap .= '</urlset>';
 $fp = fopen(ABSPATH . "cat-sitemap.xml", 'w');
 fwrite($fp, $sitemap);
 fclose($fp);
}

Step 2:) Now Create a file named it as sitemap-style.xsl.
Step 3:) Now Copy the below code and Paste in that file and save it as above name.

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:html="http://www.w3.org/TR/REC-html40"xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"><xsl:variable name="fileType"> <xsl:choose> <xsl:when test="//sitemap:url">Sitemap</xsl:when> <xsl:otherwise>SitemapIndex</xsl:otherwise> </xsl:choose> </xsl:variable><html xmlns="http://www.w3.org/1999/xhtml"><head><title><xsl:choose><xsl:when test="$fileType='Sitemap'">Sitemap</xsl:when><xsl:otherwise>Sitemap Index</xsl:otherwise></xsl:choose></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">body {font-family:Helvetica,Arial,sans-serif;font-size:68.5%;}table {border:none;border-collapse:collapse;}table {font-size:1em;width:100%;}th {text-align:left;padding:5px;}tr.stripe {background-color:#f7f7f7;}</style></head> <body> <div id="content"> <h1>XML Sitemap By EXEIdeas</h1> <div> <p><xsl:choose><xsl:when test="$fileType='Sitemap'"> This sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"></xsl:value-of> URLs</xsl:when> <xsl:otherwise>This sitemap index contains <xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"></xsl:value-of> sitemaps</xsl:otherwise></xsl:choose></p> </div><xsl:choose><xsl:when test="$fileType='Sitemap'"><xsl:call-template name="sitemapTable"/></xsl:when> <xsl:otherwise><xsl:call-template name="siteindexTable"/></xsl:otherwise></xsl:choose> </div> </body> </html> </xsl:template> <xsl:template name="siteindexTable"> <table cellpadding="3"><thead><tr><th width="50%">URL</th><th>LastChange</th></tr></thead><tbody><xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/><xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/><xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap"><tr><xsl:if test="position() mod 2 != 1"><xsl:attribute name="class">stripe</xsl:attribute></xsl:if><td><xsl:variable name="itemURL"><xsl:value-of select="sitemap:loc"/></xsl:variable><a href="{$itemURL}"><xsl:value-of select="sitemap:loc"/></a></td><td><xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/></td></tr></xsl:for-each></tbody></table> </xsl:template> <xsl:template name="sitemapTable"><table cellpadding="3"><thead><tr><th width="50%">URL</th><th>Priority</th><th>Change Frequency</th><th>LastChange</th></tr></thead><tbody><xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/><xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/><xsl:for-each select="sitemap:urlset/sitemap:url"><tr><xsl:if test="position() mod 2 != 1"><xsl:attribute name="class">stripe</xsl:attribute></xsl:if><td><xsl:variable name="itemURL"><xsl:value-of select="sitemap:loc"/></xsl:variable><a href="{$itemURL}"><xsl:value-of select="sitemap:loc"/></a></td><td><xsl:if test="string(number(sitemap:priority))!='NaN'"><xsl:value-of select="concat(sitemap:priority*100,'%')"/></xsl:if></td><td><xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/></td><td><xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/></td></tr></xsl:for-each></tbody></table> </xsl:template></xsl:stylesheet>

Step 4:) Upload the above file to your WordPress hosting main folder.
Step 5:) Publish a new post or Update any old post.
Step 6:) Now open www.yourdomain.com/cat-sitemap.xml
Step 7:) You will find out your sitemap there.

Customization:

1.) Change cat-sitemap.xml with any of your desired name if you have a pre built sitemap at same name.
2.) Use same name at sitemap-style.xsl in both steps.

Last Words:

This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.

Disclaimer: The basic code that we shared here is taken from Emrah Gunduz & All In One SEO. Rest we updated it with some more awesome features.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *