I am trying to make a custom sitemap for my WordPress blogs categories. For that I added the below code in my functions.php file and when I save it, my Blog goes white. Nothing is showing up. I removed this code by going through FTP and then all got again fine.

Now I want to make and use this code. Now can anyone help me out to fix this code?

/* ------------------------------------------------------------------------- *
 *  Custom Dynamic XML Sitemap Generator For Categories
/* ------------------------------------------------------------------------- */
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) {
    $categorydate = explode(" ", $category->category_modified);
    $sitemap .= '<url>'.
      '<loc>'. get_permalink($category->ID) .'</loc>'.
      '<lastmod>'. $categorydate[0] .'</lastmod>'.
  $sitemap .= '</urlset>';
  $fp = fopen(ABSPATH . "custom-cat-sitemap.xml", 'w');
  fwrite($fp, $sitemap);

Fist error I see is a syntax error:

depth => 0

should be

"depth" => 0

Second error is that you are using a category object like a post object. None of these lines will work:

There is no post data to setup. Remove this line:


A category object has no category_modified property. Maybe you need the date of the last published post on the category (this is another question)???

//Not valid property

get_permalink() is for posts, for categories use get_category_link() instead. Also, $category->ID is not a valid property of a category object, use $category->term_id instead:


  • Thanks. You mentioned the right thing and all is working now perfectly. Yes, I want to add the date of the last published post on the category so for this I have to ask other question or you can give answer me here too? – Muhammad Hassan Feb 1 '15 at 9:32
  • Ask aother question, it is better to maintain things clear. Make a research first, it is very probably that similar question was made before. – cybmeta Feb 1 '15 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.