Using get_sitemap_url to get the sitemap URL in WordPress

The get_sitemap_url function in WordPress is designed to retrieve the URL of the website’s sitemap. This function is part of WordPress’s core functionality and is used to interact with the built-in XML Sitemaps feature. The sitemap URL it retrieves is the one that has been generated by WordPress automatically.

The sitemap is an XML file that lists all the important pages of a WordPress site. This file improves the site’s SEO by making it easier for search engines to discover and index the site’s content. The get_sitemap_url function can be used to access the URL of this sitemap, which can then be submitted to search engines or used in other ways to improve the site’s SEO.

It is important to note that the get_sitemap_url function will only return a URL if the XML Sitemaps feature is enabled on the WordPress site. If the feature is not enabled, the function will not return a URL.

Parameters Accepted by the get_sitemap_url Function

The get_sitemap_url function in WordPress accepts a set of parameters to customize the output. Here are the details:

  • $name (string) – This is a mandatory parameter that denotes the name of the sitemap.
  • $subtype_name (string) – This is an optional parameter. If not provided, its default value is an empty string. It represents the name of the sitemap subtype.
  • $page (int) – This is also an optional parameter. If not provided, its default value is 1. It represents the page number of the sitemap.

Return Value of the get_sitemap_url Function

The get_sitemap_url function returns either a string or false. The string represents the URL of the sitemap. If the function is unable to locate the sitemap, it returns false.

If the get_sitemap_url function doesn’t accept any parameters, it will be explicitly stated.

Examples

How to get the URL of a specific sitemap

$sitemap_name = 'posts';
$sitemap_url = get_sitemap_url($sitemap_name);
echo '<p>Sitemap URL: ' . $sitemap_url . '</p>';

This code snippet gets the URL of the sitemap with the name ‘posts’. The get_sitemap_url function is used to retrieve the URL of the sitemap. The result is then displayed in a paragraph.

How to get the URL of a specific sitemap subtype

$sitemap_name = 'posts';
$subtype_name = 'post';
$sitemap_url = get_sitemap_url($sitemap_name, $subtype_name);
echo '<p>Sitemap URL: ' . $sitemap_url . '</p>';

This code snippet gets the URL of a specific sitemap subtype. In this case, the function get_sitemap_url is used to get the URL of the ‘post’ subtype of the ‘posts’ sitemap. The result is then displayed in a paragraph.

How to get the URL of a specific page of a sitemap

$sitemap_name = 'posts';
$page = 2;
$sitemap_url = get_sitemap_url($sitemap_name, '', $page);
echo '<p>Sitemap URL: ' . $sitemap_url . '</p>';

This code snippet gets the URL of a specific page of a sitemap. The function get_sitemap_url is used to get the URL of page 2 of the ‘posts’ sitemap. The result is then displayed in a paragraph.

Conclusion

The get_sitemap_url function in WordPress is a tool that allows developers to retrieve the URL of the sitemap. This function is typically used when there is a need to display the sitemap URL in the front-end of a website or when the sitemap URL needs to be used in other functions or scripts. It’s important to note that the sitemap URL only gets generated if the site visibility settings allow search engines to index the site. Therefore, the get_sitemap_url function can play a significant role in SEO and site navigation strategies.

Related WordPress Functions