How to Create an HTML Sitemap in WordPress? Computer IT Help 86 | Best Urdu & Hindi Tutorials

Ads

 

How to Create an HTML Sitemap in WordPress?
HTML sitemap makes it easier for users as well as Search Engine Bot to find content on your site.There are many plugins to create HTML sitemaps in WordPress. But I will suggest you to create your HTML Sitemap Manually, You can do it with a bit of php code, you can create an HTML sitemap in WordPress yourself and have more control over the output So that you will got more places in Search Engine Results.

Creating HTML Sitemap for WordPress

Here are Steps to follow to create sitemap for WordPress..
    • Login to Your Hosting Where You Host Your WordPress
    • Open File Manager and Find functions.php in your directory
    • Open functions.php for editing
    • Paste below code right after opening <?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' );
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.

How to Remove Pages from WordPress Sitemap? 

If you want to exclude pages from your WordPress sitemap simply remove following lines from the 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).

Note:

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends.

Post a Comment

 
Top