Save page data to an xml file

时间:2011-10-31 作者:Rob

是否可以使用wordpress页面中的数据持续更新服务器上的xml文件?如果是这样,我该怎么做?

我使用这个插件http://plugins.elliotcondon.com/advanced-custom-fields/ 创建自定义数据。我能否将其保存到xml文件中?

每次创建或编辑新页面时,都要更新此xml文件,这一点很重要。

3 个回复
最合适的回答,由SO网友:v0idless 整理而成

这个save_post 每次创建或更新帖子时都会调用挂钩。就这样吧。你可以这样做fputcsv(\'/tmp/\' . $post_id, $post) 或输出为XML/JSON。

==编辑==

add_action( \'save_post\', \'wp239s5_post_to_xml\', 10, 2 );
function wp239s5_post_to_xml( $post_id, $post ) {
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
                    return $post_id;

    //convert $post to xml here 
}

SO网友:Wyck

是的,这是可能的,我建议看看PHP的SimpleXML 功能或XMLWriter, 然后把它钩住save_post.

SO网友:Jeremy Love

我最近制作了一个站点地图,它可以将我的帖子保存到xml文件中,更改周围的字段,以获取您想要放入其中的内容,但这很可能会解决您的问题。

您要研究的函数是

 $fp = fopen(ABSPATH . "sitemap.xml", \'w\');
      fwrite($fp, $sitemap);
      fclose($fp);
这就是乐趣的开始

add_action("publish_post", "eg_create_sitemap");
add_action("publish_page", "eg_create_sitemap");

function eg_create_sitemap() {
  $postsForSitemap = get_posts(array(
    \'numberposts\' => -1,
    \'orderby\' => \'modified\',
    \'post_type\'  => array(\'post\'),
    \'order\'    => \'ASC\',
    \'post_status\'  => \'publish\'
  ));

  $sitemap = \'<?xml version="1.0" encoding="UTF-8"?>\'."\\n";
  $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" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">\'."\\n";
  /*$sitemap .= \'<?xml-stylesheet type="text/xsl" href="http://sitehere.com/sitemap.xsl"?>\'."\\n";*/

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

            $post_id = $post->ID;
            $postdat = $post->post_date;
            $postdate = explode(" ", $postdat);
            $postdatee = $postdate[0]."T".$postdate[1]."-08:00"; 
            $postContent = $post->post_excerpt;
            $postName = $post->post_name;
            $posttitle = $post->post_title;
            $postimg = get_post_meta($post_id, \'image\', true);
            $postviews = get_post_meta($post_id, \'views\', true);
            $postrating = get_post_meta($post_id, \'ratings_average\', true);
            $postCategorys = get_the_category($post_id);
            $posttags = get_the_tags($post_id);
            $post_contentloc = get_post_meta($post_id, \'enclosure\', true);
            $post_content_loc =  explode("\\n", $post_contentloc);
            $abspath = get_site_url();
            $postcat = $postCategorys[0]->category_nicename;

            $sitemap .= \'<url>\'."\\n".
              "\\t".\'<loc>\'.$abspath.\'/\'.$postcat."/".$postName.\'/</loc>\'."\\n".
              "\\t".\'<video:video>\'."\\n".
                  "\\t\\t".\'<video:thumbnail_loc>\'.$postimg.$post_custom[image].\'</video:thumbnail_loc>\'."\\n".
                  "\\t\\t".\'<video:title>\'. $posttitle .\'</video:title>\'."\\n".
                  "\\t\\t".\'<video:publication_date>\'. $postdatee .\'</video:publication_date>\'."\\n".
                  "\\t\\t".\'<video:description>\'. $postContent .\'</video:description>\'."\\n".
                  "\\t\\t".\'<video:content_loc>\'.$post_content_loc[0].\'</video:content_loc>\'."\\n".
                  "\\t\\t".\'<video:view_count>\'. $postviews .\'</video:view_count>\'."\\n".
                  "\\t\\t".\'<video:rating>\'. $postrating .\'</video:rating>\'."\\n".
                  "\\t\\t".\'<video:player_loc allow_embed="yes" autoplay="ap=1">http://sitehere.com/wp-content/uploads/jw-player-plugin-for-wordpress/player/player.swf</video:player_loc>\'."\\n".
                  "\\t\\t".\'<video:requires_subscription>no</video:requires_subscription>\'."\\n";
            if ($postCategorys) {
                  foreach($postCategorys as $postCategory) {
                    $sitemap .= "\\t\\t".\'<video:category>\'.$postCategory->cat_name.\'</video:category>\'."\\n".
                        "\\t\\t".\'<video:gallery_loc title="\'.$postCategory->cat_name.\'">\'.$abspath."/".$postCategory->category_nicename.\'</video:gallery_loc>\'."\\n";
                  }

                }

            if ($posttags) {
                  foreach($posttags as $tag) {
                    $sitemap .= "\\t\\t".\'<video:tag>\'.$tag->name . \' \'.\'</video:tag>\'."\\n"; 
                  }
                }

            $sitemap .="\\t\\t".\'<video:uploader info="http://sitehere.com">blah blah</video:uploader>\'."\\n".
                    "\\t\\t".\'<video:family_friendly>yes</video:family_friendly>\'."\\n".
                "\\t".\'</video:video>\'."\\n".  
            \'</url>\'."\\n\\n";
  }

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

  $fp = fopen(ABSPATH . "sitemap.xml", \'w\');
  fwrite($fp, $sitemap);
  fclose($fp);
}

?>

结束