How to create a HTML sitemap in wordpress:
Follow the given steps to create a sitemap:
1. First log in to your hosting account where you have installed your wordpress.
2. Open the File Manager and find function.php in your directory.
3. Open function.php for editing.
4. Paste the below given code just after <?php tag.
The Above code will list all of your pages and posts into your sitemap and sort out all posts category wise. A single post will be displayed once no matter if the post published under multiple categories.I hope you understand it.
3. Open function.php for editing.
4. Paste the below given code just after <?php tag.
function spp_html_sitemap() {
$spp_sitemap = '';
$published_posts = wp_count_posts('post');
$spp_sitemap .= '<h4 id="sitemap-posts-h4">Here is a list of of my '.$published_posts->publish.' published posts</h4>';
$args = array(
'exclude' => '', /* ID of categories to be excluded, separated by comma */
'post_type' => 'post',
'post_status' => 'publish'
);
$cats = get_categories($args);
foreach ($cats as $cat) :
$spp_sitemap .= '<div>';
$spp_sitemap .= '<h3>Category: <a href="'.get_category_link( $cat->term_id ).'">'.$cat->cat_name.'</a></h3>';
$spp_sitemap .= '<ul>';
query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
while(have_posts()) {
the_post();
$category = get_the_category();
/* Only display a post link once, even if it's in multiple categories */
if ($category[0]->cat_ID == $cat->cat_ID) {
$spp_sitemap .= '<li class="cat-list"><a href="'.get_permalink().'" rel="bookmark">'.get_the_title().'</a></li>';
}
}
$spp_sitemap .= '</ul>';
$spp_sitemap .= '</div>';
endforeach;
$pages_args = array(
'exclude' => '', /* ID of pages to be excluded, separated by comma */
'post_type' => 'page',
'post_status' => 'publish'
);
$spp_sitemap .= '<h3>Pages</h3>';
$spp_sitemap .= '<ul>';
$pages = get_pages($pages_args);
foreach ( $pages as $page ) :
$spp_sitemap .= '<li class="pages-list"><a href="'.get_page_link( $page->ID ).'" rel="bookmark">'.$page->post_title.'</a></li>';
endforeach;
$spp_sitemap .= '<ul>';
return $spp_sitemap;
}
add_shortcode( 'spp-sitemap','spp_html_sitemap' );
The Above code will list all of your pages and posts into your sitemap and sort out all posts category wise. A single post will be displayed once no matter if the post published under multiple categories.I hope you understand it.
How to remove pages from Wordpress sitemap:
If you want to remove pages from wordpress sitemap just remove the following given code from above given code:
$pages_args = array(
'exclude' => '', /* ID of pages to be excluded, separated by comma */
'post_type' => 'page',
'post_status' => 'publish'
);
$spp_sitemap .= '<h3>Pages</h3>';
$spp_sitemap .= '<ul>';
$pages = get_pages($pages_args);
foreach ( $pages as $page ) :
$spp_sitemap .= '<li class="pages-list"><a href="'.get_page_link( $page->ID ).'" rel="bookmark">'.$page->post_title.'</a></li>';
endforeach;
$spp_sitemap .= '<ul>';
You can also exclude certain pages and categories by entering their ID(mentioned in comments of above code).
I hope now you are able to make your own sitemap easily.If you like this article share it on social media to help other blogger brothers.
Let me know if you have any problem regarding to this article.If you have any problem you can comment me below.Thanks for reading this article.Stay tuned for more articles.