The XML Sitemap & Google News plugin adds two conditional checks that can be useful for theme or plugin developers.
is_sitemap()
Returns true when the current query is for any XML sitemap, including index and Google News sitemaps.
Source
/**
* Is the query for a sitemap?
*
* @since 4.8
* @return bool
*/
function is_sitemap() {
if ( function_exists( 'wp_sitemaps_loaded' ) ) {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return property_exists( $wp_query, 'is_sitemap' ) ? $wp_query->is_sitemap : false;
}
global $xmlsf;
if ( ! is_object( $xmlsf ) || false === $xmlsf->request_filtered ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional sitemap tags do not work before the sitemap request filter is run. Before then, they always return false.', 'xml-sitemap-feed' ), '4.8' );
return false;
}
return $xmlsf->is_sitemap;
}
is_news()
Returns true when the current query is for a Google News sitemap.
Source
/**
* Is the query for a news sitemap?
*
* @since 4.8
* @return bool
*/
function is_news() {
global $xmlsf;
if ( ! is_object( $xmlsf ) || false === $xmlsf->request_filtered_news ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional sitemap tags do not work before the sitemap request filter is run. Before then, they always return false.', 'xml-sitemap-feed' ), '4.8' );
return false;
}
return $xmlsf->is_news;
}