I have a WordPress website in which the content is dynamically generated when a user scroll down new data comes from the database with the increment in the page variable of the page for example like this
http://testsite.com/?page=2
Now i want to create the sitemap so that i could indexed all my dynamically generated pages
I have tried to do so but the problem is that on every scroll down I have created sitemap is there any elegant way to do that. I have placed the sitemap code behind the on scroll jQuery event
The problem is with this code the sitemap is created on every scroll with duplicate links too
$sitemap .= '<?xml version="1.0" encoding="UTF-8"?>' . '<?xml-stylesheet type="text/xsl" href="' .
esc_url( home_url( '/' ) ) . 'sitemap.xsl"?>';
$sitemap .= "
" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "
";
$sitemap .= "\t" . '<url>' . "
" .
"\t\t" . '<loc>' . esc_url( home_url( '/' ) ) . '</loc>' .
"
\t\t" . '<lastmod>' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . '</lastmod>' .
"
\t\t" . '<changefreq>daily</changefreq>' .
"
\t\t" . '<priority>1.0</priority>' .
"
\t" . '</url>' . "
";
foreach( $result as $row ) {
$sitemap .= "\t" . '<url>' . "
" .
"\t\t" . '<loc>' . $row->ID . '</loc>' .
"
\t\t" . '<lastmod>' . the_time( 'l, F jS, Y' ). '</lastmod>' .
"
\t\t" . '<changefreq>Daily</changefreq>' .
"
\t\t" . '<priority>0.9</priority>' .
"
\t" . '</url>' . "
";
}
$sitemap .= '</urlset>';
$fp = fopen( ABSPATH . "sitemap.xml", 'w' );
fwrite( $fp, $sitemap );
fclose( $fp );