What is a Sitemap?
A sitemap provides information about the posts, pages and other content on your site are intended to help visitors find specific pages, posts and other information about the website and also be used by crawlers.
There are a couple of different types of sitemaps, such as
- Visual one that you can create before building a website by its designers.
- Human visible listings, typically hierarchical, of the pages on a site(HTML).
- Structured listings intended for web crawlers such as search engines(XML).
***Please scroll down to see complete code block***
How to create XML Sitemap
Before showing you how to create XML sitemap, I need to describe the XML schema for the Sitemap protocol. The Sitemap must:
- Begin with an opening
<urlset>tag and end with a closing</urlset>tag. - Specify the namespace (protocol standard) within the
<urlset>tag. - Include a
<url>entry for each URL, as a parent XML tag. - Include a
<loc>child entry for each<url>parent tag.
All other tags are optional. Optional tags are <lastmod>,<changefreq> and <priority>.
Sample XML Sitemap
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://wpartisan.net/contact-us/</loc>
<lastmod>2020-01-15</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
Some important information about XML tags:
<loc> : This value must be less than 2,048 characters.
<lastmod> : The date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.
<changefreq> : Valid values are:
- always
- hourly
- daily
- weekly
- monthly
- yearly
- never
<priority> : Valid values range from 0.0 to 1.0.
Please check here what sitemaps.org has to say about sitemaps.
Here is the complete code to generate XML Sitemap
Step 1: Open the functions.php file from the theme located in the folder (wp-content/themes/your_theme_folder/functions.php).
Step 2: Copy and paste the below code into the functions.php file:
add_action( 'init', 'wpartisan_xml_sitemap_initialization' );
function wpartisan_xml_sitemap_initialization(){
global $all_post_type_names;
$default_posts_types = array(
'page' => 'page',
'post' => 'post',
);
$args = array(
'public' => true,
'_builtin' => false,
);
$custom_post_types = get_post_types( $args, 'names' );
$all_post_types = array_merge( $default_posts_types, $custom_post_types );
$all_post_type_names = array_keys( $all_post_types );
if( !empty( $all_post_type_names ) ) {
foreach( $all_post_type_names as $post_type_name ):
add_action( 'publish_'.$post_type_name, 'xml_sitemap' );
endforeach;
}
}
function xml_sitemap() {
global $all_post_type_names;
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach ( $all_post_type_names as $pty_name ) :
if( $pty_name === 'page' ):
$args = array(
'sort_order' => 'desc',
'sort_column' => 'post_modified',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => ''.$pty_name.'',
'post_status' => 'publish'
);
$postsForSitemap = get_pages($args);
else:
$args = array(
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'DESC',
'exclude' => '',
'post_type' => ''.$pty_name.'',
);
$postsForSitemap = get_posts($args);
endif;
if( !empty( $postsForSitemap ) ){
foreach($postsForSitemap as $post) {
setup_postdata($post);
$postdate = explode(" ", $post->post_modified);
$sitemap .= '<url>'.
'<loc>'. get_permalink($post->ID) .'</loc>'.
'<lastmod>'. $postdate[0] .'</lastmod>'.
'<changefreq>monthly</changefreq>'.
'</url>';
}
}
endforeach;
$sitemap .= '</urlset>';
$fp = fopen(ABSPATH . "sitemap.xml", 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
How to create HTML Sitemap
An HTML sitemap is a human-visible listing, typically hierarchical, of the pages on a site.
Step 1: Open the functons.php file from the theme located in the folder (wp-content/themes/your_theme_folder/functions.php).
Step 2: Copy and paste the below code into the functions.php file: Here, I provide a shortcode to display the listing.. add_shortcode() is WordPress built-in function for shortcode tags. This function requires two parameters. First is your shortcode, and the second is your PHP function name which will generate the output. You can use your shortcode on a page or post like [your_shortcode_name].
add_shortcode( 'rsxh_sitemap', 'rsxh_sitemap_func' );
function rsxh_sitemap_func( $atts ) {
$default_posts_types = array(
'page' => 'page',
'post' => 'post',
);
$args = array(
'public' => true,
'_builtin' => false,
);
$custom_post_types = get_post_types( $args, 'names' );
$all_post_types = array_merge( $default_posts_types, $custom_post_types );
$all_post_type_names = array_keys( $all_post_types );
$post_types = implode( ',', $all_post_type_names );
$params = shortcode_atts( array(
'post_types' => $post_types,
), $atts );
ob_start();
if( !empty( $params ) ):
$output = '';
if( !empty( $params['post_types'] ) ):
$post_types = explode( ',', $params['post_types'] );
foreach ( $post_types as $pty_name ) :
if( $pty_name === 'page' ):
$output .= wp_list_pages();
else:
$args = array(
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'DESC',
'exclude' => '',
'post_type' => ''.$pty_name.'',
);
$postsForSitemap = get_posts($args);
if( !empty( $postsForSitemap ) ){
$output .= '<li class="'.$pty_name.'nav">'.ucfirst($pty_name);
$output .= '<ul class="'.$pty_name.'">';
foreach($postsForSitemap as $post) {
setup_postdata($post);
$output .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></li>';
}
$output .= '</ul>';
$output .= '</li>';
}
endif;
endforeach;
endif;
endif;
$output .= ob_get_clean();
return $output;
}
Step 3 : Add shortcode to any pages or posts from wp-admin.
In the last step, you have to create/edit a page or post from your wp-admin panel and place the shortcode [your_shortcode_name] (here, the shortcode name is rsxh_sitemap ) in the content area section and save.
Create a Sitemap With Plugin
Really Simple XML and HTML Sitemap Plugin for WordPress
This WordPress plugin provides an easy way to add a sitemap to your pages, posts and custom post type(CPT). To show HTML Sitemap on any pages or posts, just add a shortcode from add/edit screen or echo a shortcode on your template file.
It also generates an XML sitemap which will be created/placed on your site root directory (file name sitemap.xml). XML sitemaps help search engines like Google and Bing index your site more effectively.
Plugins to Enhance Your WooCommerce Store
Carefully selected tools to help you customize, manage, and optimize your WooCommerce experience.
Woo Parent Category Selector
- Automatically assign parent categories when child categories are selected
- Prevent accidental removal of required parent categories
- Automatically repairs missing parent category assignments during product save
- Bulk repair existing WooCommerce product category structures
- Keeps product category structures organized and consistent
Product Catalog Mode For WooCommerce
- Turn your WooCommerce store into catalog mode instantly
- Hide Add to Cart buttons and product prices conditionally
- Apply rules by user role, country, or scheduled time periods
- Display custom messages and inquiry options for products
- Control visibility settings globally or per product
Remove Add to Cart Button for WooCommerce
- Remove Add to Cart buttons from shop and product pages
- Hide buttons conditionally for selected products or categories
- Replace Add to Cart with custom text or messages
- Control button visibility by user role and product type
- Lightweight settings interface with easy customization
Remove Product Content for WooCommerce
- Hide product title, image, price, rating, and descriptions
- Remove tabs, meta information, SKU, tags, and related products
- Control visibility separately for shop and single product pages
- Customize WooCommerce layouts without coding
- Enable or disable content elements with simple settings
Really Simple XML and HTML Sitemap
- Generate both XML and HTML sitemaps automatically
- Display sitemaps anywhere using ShortCode support
- Include pages, posts, and custom post types
- Exclude selected pages or posts easily
- Supports WPML and Polylang multilingual plugins



