如何在没有插件的情况下生成/更新XML站点地图?

时间:2011-04-20 作者:João

我喜欢在我的WordPress网站上硬编码所有内容,而不使用任何插件。是否有任何方法可以在每次我在多站点博客上发布/更新帖子时生成或更新我的站点地图,而不使用插件?

4 个回复
SO网友:drjorgepolanco

以下代码可以立即运行。您的网站地图将显示在:https://your-website-name.com/sitemap.xml

每次创建或更新页面、帖子或自定义帖子类型时,它都会显示出来。确保添加自定义帖子类型的名称:

add_action( \'publish_post\', \'ow_create_sitemap\' );
add_action( \'publish_page\', \'ow_create_sitemap\' );
add_action( \'save_post\',    \'ow_create_sitemap\' );

function ow_create_sitemap() {
    $postsForSitemap = get_posts(array(
        \'numberposts\' => -1,
        \'orderby\'     => \'modified\',
        // \'custom_post\' should be replaced with your own Custom Post Type (one or many)
        \'post_type\'   => array( \'post\', \'page\', \'custom_post\' ),
        \'order\'       => \'DESC\'
    ));

    $sitemap = \'<?xml version="1.0" encoding="UTF-8"?>\';
    $sitemap .= \'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 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">\';

    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post );

        $postdate = explode( " ", $post->post_modified );

        $sitemap .= \'<url>\'.
                    \'<loc>\' . get_permalink( $post->ID ) . \'</loc>\' .
                    \'<lastmod>\' . $postdate[0] . \'</lastmod>\' .
                    \'<changefreq>monthly</changefreq>\' .
                    \'</url>\';
      }

    $sitemap .= \'</urlset>\';

    $fp = fopen( ABSPATH . \'sitemap.xml\', \'w\' );

    fwrite( $fp, $sitemap );
    fclose( $fp );
}

SO网友:w3uiguru

我不知道这是否适用于多站点,但对我来说,它在一个WordPress安装中运行得非常完美。

当您创建/更新任何帖子或页面时,它将生成一个站点地图。xml文件,并使用最新的第一个(最后修改的)更新链接(URL)。

在活动主题的函数中复制并粘贴以下代码。php文件:

/* function to create sitemap.xml file in root directory of site  */        
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");  
add_action( "save_post", "eg_create_sitemap" );   
function eg_create_sitemap() {
    $postsForSitemap = get_posts( array(
        \'numberposts\' => -1,
        \'orderby\'     => \'modified\',
        \'post_type\'   => array( \'post\', \'page\' ),
        \'order\'       => \'DESC\'
    ) );
    $sitemap = \'<?xml version="1.0" encoding="UTF-8"?>\';
    $sitemap .= "\\n" . \'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\' . "\\n";    
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post );   
        $postdate = explode( " ", $post->post_modified );   
        $sitemap .= "\\t" . \'<url>\' . "\\n" .
            "\\t\\t" . \'<loc>\' . get_permalink( $post->ID ) . \'</loc>\' .
            "\\n\\t\\t" . \'<lastmod>\' . $postdate[0] . \'</lastmod>\' .
            "\\n\\t\\t" . \'<changefreq>monthly</changefreq>\' .
            "\\n\\t" . \'</url>\' . "\\n";
    }     
    $sitemap .= \'</urlset>\';     
    $fp = fopen( ABSPATH . "sitemap.xml", \'w\' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}

SO网友:Locutor Antonio Cezar

在使用w3uiguru的答案中提供的代码之前,我必须按照公认的XML文件标准进行一些改进。代码如下:

/* function to create sitemap.xml file in root directory of site  */
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
    if ( str_replace( \'-\', \'\', get_option( \'gmt_offset\' ) ) < 10 ) { 
        $tempo = \'-0\' . str_replace( \'-\', \'\', get_option( \'gmt_offset\' ) ); 
    } else { 
        $tempo = get_option( \'gmt_offset\' ); 
    }
    if( strlen( $tempo ) == 3 ) { $tempo = $tempo . \':00\'; }
    $postsForSitemap = get_posts( array(
        \'numberposts\' => -1,
        \'orderby\'     => \'modified\',
        \'post_type\'   => array( \'post\', \'page\' ),
        \'order\'       => \'DESC\'
    ) );
    $sitemap .= \'<?xml version="1.0" encoding="UTF-8"?>\' . \'<?xml-stylesheet type="text/xsl" href="\' . 
        esc_url( home_url( \'/\' ) ) . \'sitemap.xsl"?>\';
    $sitemap .= "\\n" . \'<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">\' . "\\n";
    $sitemap .= "\\t" . \'<url>\' . "\\n" .
        "\\t\\t" . \'<loc>\' . esc_url( home_url( \'/\' ) ) . \'</loc>\' .
        "\\n\\t\\t" . \'<lastmod>\' . date( "Y-m-d\\TH:i:s", current_time( \'timestamp\', 0 ) ) . $tempo . \'</lastmod>\' .
        "\\n\\t\\t" . \'<changefreq>daily</changefreq>\' .
        "\\n\\t\\t" . \'<priority>1.0</priority>\' .
        "\\n\\t" . \'</url>\' . "\\n";
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post);
        $postdate = explode( " ", $post->post_modified );
        $sitemap .= "\\t" . \'<url>\' . "\\n" .
            "\\t\\t" . \'<loc>\' . get_permalink( $post->ID ) . \'</loc>\' .
            "\\n\\t\\t" . \'<lastmod>\' . $postdate[0] . \'T\' . $postdate[1] . $tempo . \'</lastmod>\' .
            "\\n\\t\\t" . \'<changefreq>Weekly</changefreq>\' .
            "\\n\\t\\t" . \'<priority>0.5</priority>\' .
            "\\n\\t" . \'</url>\' . "\\n";
    }
    $sitemap .= \'</urlset>\';
    $fp = fopen( ABSPATH . "sitemap.xml", \'w\' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}

SO网友:t3chernobyl

在寻找一个非常具体的用例时,我稍微修改了@Loctor antonio cezar的代码。我需要一个专门为谷歌新闻写的网站地图。有什么不同?整个标记如下the rules. 就我的具体情况而言,我将帖子数量限制在20个。超过2天的帖子也会消失。也许有人需要这个:

/* function to create sitemap.xml file in root directory of site  */
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
    if ( str_replace( \'-\', \'\', get_option( \'gmt_offset\' ) ) < 10 ) { 
        $tempo = \'-0\' . str_replace( \'-\', \'\', get_option( \'gmt_offset\' ) ); 
    } else { 
        $tempo = get_option( \'gmt_offset\' ); 
    }
    if( strlen( $tempo ) == 3 ) { $tempo = $tempo . \':00\'; }
    $postsForSitemap = get_posts( array(
        \'numberposts\' => 20,
        \'orderby\'     => \'modified\',
        \'post_type\'   => array( \'post\', \'page\' ),
        \'order\'       => \'DESC\',
        \'date_query\' => array(
            \'after\' => date(\'Y-m-d\', strtotime(\'-2 days\')) 
        )
    ) );
    $sitemap .= \'<?xml version="1.0" encoding="UTF-8"?>\';
    $sitemap .= "\\n" . \'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">\' . "\\n";
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post);
        $postdate = explode( " ", $post->post_modified );
        $sitemap .= "\\t" . "<url>" . "\\n";
        $sitemap .= "\\t\\t" . "<loc>" . get_permalink( $post->ID ) . \'</loc>\';
        $sitemap .= "\\t\\t" . \'<news:news>\' . "\\n";
        $sitemap .= "\\t\\t\\t" . \'<news:publication>\' . "\\n";
        $sitemap .= "\\t\\t\\t\\t" . \'<news:name><![CDATA[ YOUR SITE ]]></news:name>\' . "\\n";
        $sitemap .= "\\t\\t\\t\\t" . \'<news:language>YOUR LANGUAGE</news:language>\' . "\\n";
        $sitemap .= "\\t\\t\\t" . \'</news:publication>\' . "\\n";
        $sitemap .= "\\t\\t\\t<news:publication_date>" . $postdate[0] . \'T\' . $postdate[1] . $tempo . "</news:publication_date>\\n";
        $sitemap .= "\\t\\t\\t" . \'<news:title><![CDATA[\' . get_the_title( $post) . \']]></news:title>\' . "\\n";
        $sitemap .= "\\t\\t" . \'</news:news>\' . "\\n";
        $sitemap .= "\\t" . \'</url>\' . "\\n";
    }
    $sitemap .= \'</urlset>\';
    $fp = fopen( ABSPATH . "sitemap_news.xml", \'w\' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?