duanao2585 2019-06-17 07:00
浏览 83

在WordPress中为动态生成的内容创建站点地图的优雅方式

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 );
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)