function build_baidu_sitemap_html() { global $wpdb; /** Get the current time **/ $blogtime = current_time('mysql'); list($today_year, $today_month, $today_day, $hour, $minute, $second) = split('([^0-9])', $blogtime); ##文章 $html_contents = ''; $post = query_posts('ignore_sticky_posts=1&posts_per_page=1000'); while (have_posts()) { the_post(); $html_contents .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '" target="_blank">' . get_the_title() . '</a></li>'; } $html_category_contents = wp_list_categories('echo=0'); $html_page_contents = wp_list_pages('echo=0'); $blog_title = __('SiteMap', 'baidu_sitemap'); $blog_name = get_bloginfo('name'); $blog_keywords = $blog_title . ',' . $blog_name; $lc_generator = 'Baidu SiteMap Generator'; $lc_author = 'Liucheng.Name'; $lc_copyright = 'Liucheng.Name'; $blog_home = get_bloginfo('url'); $sitemap_url = get_bloginfo('url') . '/sitemap.html'; $recentpost = __('RecentPost', 'baidu_sitemap'); $footnote = __('HomePage', 'baidu_sitemap'); $updated_time = "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}"; if ($html_contents) { $path_html = GetPluginPath() . 'sitemap.html'; $html = file_get_contents("{$path_html}"); $html = str_replace("%blog_title%", $blog_title, $html); $html = str_replace("%blog_name%", $blog_name, $html); $html = str_replace("%blog_home%", $blog_home, $html); $html = str_replace("%blog_keywords%", $blog_keywords, $html); $html = str_replace("%lc_generator%", $lc_generator, $html); $html = str_replace("%lc_author%", $lc_author, $html); $html = str_replace("%lc_copyright%", $lc_copyright, $html); $html = str_replace("%sitemap_url%", $sitemap_url, $html); $html = str_replace("%footnote%", $footnote, $html); $html = str_replace("%RecentPost%", $recentpost, $html); $html = str_replace("%updated_time%", $updated_time, $html); $html = str_replace("%contents%", $html_contents, $html); $html = str_replace("%Lc_category_contents%", $html_category_contents, $html); $html = str_replace("%Lc_page_contents%", $html_page_contents, $html); $GetHomePath = GetHomePath(); $filename_html = $GetHomePath . 'sitemap.html'; if (IsFileWritable($GetHomePath) || IsFileWritable($filename_html)) { file_put_contents("{$filename_html}", "{$html}"); @chmod($filename_html, 0777); /** Messages **/ /*baidu_sitemap_topbarmessage(__('Congratulate, Build the Html file success','baidu_sitemap'));*/ } } }
/** build the XML file, sitemap_baidu.xml **/ function build_baidu_sitemap() { global $wpdb, $posts, $wp_version; $get_baidu_sitemap_options = get_option(BAIDU_SITEMAP_OPTION); if (!empty($get_baidu_sitemap_options)) { list($lc_blog_url, $lc_admin_email, $lc_updatePeri, $lc_limits, $lc_sitemap_auto, $lc_order_1, $lc_order_2, $lc_order_3, $lc_comments, $lc_post_length, $lc_post_cat, $lc_post_views, $lc_pickcats, $lc_comments_count, $lc_views_count, $lc_sitemap_html, $lc_sitemap_publish_post, $lc_support, $lc_baidu_sitemap_by_post) = explode("|", $get_baidu_sitemap_options); } $lc_pickcats_array = explode(";", $lc_pickcats); $blog_home = get_bloginfo('url'); /** Get the current time **/ $blogtime = current_time('mysql'); list($today_year, $today_month, $today_day, $hour, $minute, $second) = split('([^0-9])', $blogtime); /** XML_begin **/ $xml_begin = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; $xml_begin .= '<document xmlns:bbs="http://www.baidu.com/search/bbs_sitemap.xsd">' . "\n"; $xml_begin .= xml_annotate(); $xml_begin .= '<webSite>' . "{$lc_blog_url}" . '</webSite>' . "\n"; $xml_begin .= '<webMaster>' . "{$lc_admin_email}" . '</webMaster>' . "\n"; $xml_begin .= '<updatePeri>' . "{$lc_updatePeri}" . '</updatePeri>' . "\n"; $xml_begin .= '<updatetime>' . "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}" . '</updatetime>' . "\n"; $xml_begin .= '<version>' . "WordPress" . '</version>' . "\n"; //echo $xml_begin; /** get the post title,ID,post_date from database **/ $sql = "SELECT DISTINCT ID\r\n\t\tFROM {$wpdb->posts}\r\n\t\tLEFT JOIN {$wpdb->comments} ON ( {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID ) \r\n\t\tWHERE post_password = ''\r\n\t\tAND post_type = 'post'\r\n\t\tAND post_status = 'publish'\r\n\t\tORDER BY {$lc_order_1} DESC \r\n\t\tLIMIT 0,{$lc_limits}"; $recentposts = $wpdb->get_results($sql); if ($recentposts) { foreach ($recentposts as $post) { /** Post URL **/ $permalink = EscapeXML(stripslashes_deep(get_permalink($post->ID))); if ($lc_blog_url) { $permalink = str_replace("{$blog_home}", "{$lc_blog_url}", $permalink); } //2009-11-25 /** Post **/ $my_post = get_post($post->ID, ARRAY_A); $post_title = EscapeXML(stripslashes_deep($my_post['post_title'])); $post_date = $my_post['post_date']; /** show the comments info **/ if ($lc_comments == '1') { $comment_count = $my_post['comment_count']; $comment_array = get_approved_comments($post->ID); if ($comment_array) { $last_comment = array_pop($comment_array); $my_comment = get_comment($last_comment->comment_ID, ARRAY_A); $comment_date = $my_comment['comment_date']; } else { $comment_date = $post_date; } } /** show the post_length **/ if ($lc_post_length == '1') { $post_content_str = strlen($my_post['post_content']); } /** show the cat name **/ if ($lc_post_cat == '1') { $category = get_the_category($post->ID); if (count($category) == '1') { $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name)); } if (count($category) == '2') { $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name)); } if (count($category) == '3') { $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name . "," . $category[2]->cat_name)); } if (count($category) == '4') { $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name . "," . $category[2]->cat_name . "," . $category[3]->cat_name)); } if (count($category) == '5') { $my_cat = EscapeXML(stripslashes_deep($category[0]->cat_name . "," . $category[1]->cat_name . "," . $category[2]->cat_name . "," . $category[3]->cat_name . "," . $category[4]->cat_name)); } } /** show the post_views **/ if (function_exists('the_views')) { //echo "YES"; //echo $post->ID."#"; $sql_views = "SELECT DISTINCT meta_value as views\r\n\t\t\t\tFROM {$wpdb->postmeta}\r\n\t\t\t\tWHERE post_id = '{$post->ID}'\r\n\t\t\t\tAND meta_key = 'views'\r\n\t\t\t\tLIMIT 0,1"; $post_views_array = $wpdb->get_results($sql_views); if ($post_views_array || !empty($post_views_array)) { foreach ($post_views_array as $post_views) { if ($post_views) { $post_views = number_format_i18n(intval($post_views->views)); } else { $post_views = '1'; } } } else { $post_views = '1'; } } //echo $post_views.";"; /** the pick post **/ if (!empty($lc_pickcats_array) && $lc_pickcats_array[0] != '0' || !empty($lc_comments_count) && $lc_comments_count != '0' || !empty($lc_views_count) && ${$lc_views_count} != '0') { $pick = 0; if ($pick == '0') { if (!empty($lc_pickcats_array) && $lc_pickcats_array[0] != '0') { $cat_ID = array(); foreach (get_the_category($post->ID) as $category) { array_push($cat_ID, $category->cat_ID); if (array_intersect($cat_ID, $lc_pickcats_array)) { $pick = '1'; } } } } if ($pick == '0') { if (!empty($lc_comments_count) && $lc_comments_count != '0') { if (empty($comment_count)) { $comment_count = $my_post['comment_count']; } //get post_comment_count if ($comment_count - $lc_comments_count >= '1') { $pick = '1'; } } } if ($pick == '0') { if (!empty($lc_views_count) && function_exists('the_views') && $lc_views_count != '0') { if ($post_views - $lc_views_count >= '1') { $pick = '1'; } } } } //echo $pick."; "; $xml_middle = '<item>' . "\n"; //$xml_middle .= '<link>'."$lc_blog_url".'/?p='."$post_ID".'</link>'."\n"; $xml_middle .= '<link>' . $permalink . '</link>' . "\n"; $xml_middle .= '<title>' . "{$post_title}" . '</title>' . "\n"; $xml_middle .= '<pubDate>' . "{$post_date}" . '</pubDate>' . "\n"; if ($lc_comments == '1') { $xml_middle .= '<bbs:lastDate>' . $comment_date . '</bbs:lastDate>' . "\n"; $xml_middle .= '<bbs:reply>' . $comment_count . '</bbs:reply>' . "\n"; } if (function_exists('the_views') && $lc_post_views == '1') { $xml_middle .= '<bbs:hit>' . $post_views . '</bbs:hit>' . "\n"; } if ($lc_post_length == '1') { $xml_middle .= '<bbs:mainLen>' . $post_content_str . '</bbs:mainLen>' . "\n"; } if ($lc_post_cat == '1') { $xml_middle .= '<bbs:boardid>' . $my_cat . '</bbs:boardid>' . "\n"; } if (!empty($lc_pickcats_array) && $lc_pickcats_array[0] != '0' || !empty($lc_comments_count) && $lc_comments_count != '0' || !empty($lc_views_count) && ${$lc_views_count} != '0') { $xml_middle .= '<bbs:pick>' . $pick . '</bbs:pick>' . "\n"; } $xml_middle .= '</item>' . "\n"; $xml_middle_done .= $xml_middle; /** html_contents **/ $comment_count = $my_post['comment_count']; if ($comment_count || $comment_count == 0) { $html_comment = str_replace("%html_comment%", $comment_count, __('Comments %html_comment%', 'baidu_sitemap')); } if ($post_views) { $html_views = str_replace("%html_views%", $post_views, __(' Views %html_views%', 'baidu_sitemap')); } else { $html_views = ''; } $html_content = '<li><a href="' . $permalink . '">' . $post_title . '</a> (' . $html_comment . '' . $html_views . ')</li>'; $html_contents .= $html_content . "\n"; } } /** XML_end **/ $xml_end = '</document>'; /** XML_ALL **/ $baidu_xml = $xml_begin . $xml_middle_done . $xml_end; /** save XML file as sitemap_baidu.xml **/ $GetHomePath = GetHomePath(); $filename = $GetHomePath . 'sitemap_baidu.xml'; if (IsFileWritable($GetHomePath) || IsFileWritable($filename)) { file_put_contents("{$filename}", "{$baidu_xml}"); /** Messages **/ baidu_sitemap_topbarmessage(__('Congratulate, Build the XML file success', 'baidu_sitemap')); } else { /** Messages **/ baidu_sitemap_topbarmessage(__('Directory is not writable. please chmod your directory to 777.', 'baidu_sitemap')); } /** html sitemap Page **/ if ($lc_sitemap_html = '1') { $blog_title = __('SiteMap', 'baidu_sitemap'); $blog_name = get_bloginfo('name'); $blog_keywords = $blog_title . ',' . $blog_name; $lc_generator = 'Baidu SiteMap Generator'; $lc_author = 'Liucheng.Name'; $lc_copyright = 'Liucheng.Name'; $blog_home = get_bloginfo('url'); $sitemap_url = get_bloginfo('url') . '/sitemap.html'; $recentpost = __('RecentPost', 'baidu_sitemap'); $footnote = __('HomePage', 'baidu_sitemap'); $updated_time = "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}"; if ($html_contents) { $path_html = GetPluginPath() . 'sitemap.html'; $html = file_get_contents("{$path_html}"); $html = str_replace("%blog_title%", $blog_title, $html); $html = str_replace("%blog_name%", $blog_name, $html); $html = str_replace("%blog_home%", $blog_home, $html); $html = str_replace("%blog_keywords%", $blog_keywords, $html); $html = str_replace("%lc_generator%", $lc_generator, $html); $html = str_replace("%lc_author%", $lc_author, $html); $html = str_replace("%lc_copyright%", $lc_copyright, $html); $html = str_replace("%sitemap_url%", $sitemap_url, $html); $html = str_replace("%footnote%", $footnote, $html); $html = str_replace("%RecentPost%", $recentpost, $html); $html = str_replace("%updated_time%", $updated_time, $html); $html = str_replace("%contents%", $html_contents, $html); $filename_html = $GetHomePath . 'sitemap.html'; if (IsFileWritable($GetHomePath) || IsFileWritable($filename_html)) { file_put_contents("{$filename_html}", "{$html}"); /** Messages **/ //baidu_sitemap_topbarmessage(__('Congratulate, Build the XML file success','baidu_sitemap')); } } //echo $html; } if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('do_this_auto'); } if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('do_baidu_sitemap_2'); } baidu_sitemap_is_auto(); }
function build_sitemap() { global $wpdb, $posts; $blog_name = get_bloginfo('name'); $blog_home = get_bloginfo('url'); $blog_description = get_option('blogdescription'); $blog_keywords = get_option('xiaogen_keywords') . ",站点地图"; list($year, $month, $day) = explode('-', get_option('xiaogen_wp_date')); $blog_wp_year = $year . "-" . date('Y'); ##XML生成 $sql_mini = "select ID,post_modified_gmt,post_date_gmt,post_type FROM {$wpdb->posts}\n WHERE post_password = ''\n AND (post_type != 'revision' AND post_type != 'attachment' AND post_type != 'nav_menu_item')\n AND post_status = 'publish'\n ORDER BY post_modified_gmt DESC\n LIMIT 0,1000\n "; $recentposts_mini = $wpdb->get_results($sql_mini); if ($recentposts_mini) { ##获取文章 foreach ($recentposts_mini as $post) { if ($post->post_type == 'page') { $loc = get_page_link($post->ID); if ($post->post_modified_gmt == '0000-00-00 00:00:00') { $post_date_gmt = $post->post_date_gmt; } else { $post_date_gmt = $post->post_modified_gmt; } $lastmod = date("Y-m-d\\TH:i:s+00:00", GetTimestampFromMySql($post_date_gmt)); $changefreq = 'weekly'; $priority = '0.3'; $xml_contents_page .= "<url>"; $xml_contents_page .= "<loc>{$loc}</loc>"; $xml_contents_page .= "<lastmod>{$lastmod}</lastmod>"; $xml_contents_page .= "<changefreq>{$changefreq}</changefreq>"; $xml_contents_page .= "<priority>{$priority}</priority>"; $xml_contents_page .= "</url>"; } else { $loc = get_permalink($post->ID); if ($post->post_modified_gmt == '0000-00-00 00:00:00') { $post_date_gmt = $post->post_date_gmt; } else { $post_date_gmt = $post->post_modified_gmt; } $lastmod = date("Y-m-d\\TH:i:s+00:00", GetTimestampFromMySql($post_date_gmt)); $changefreq = 'monthly'; $priority = '0.6'; $xml_contents_post .= "<url>"; $xml_contents_post .= "<loc>{$loc}</loc>"; $xml_contents_post .= "<lastmod>{$lastmod}</lastmod>"; $xml_contents_post .= "<changefreq>{$changefreq}</changefreq>"; $xml_contents_post .= "<priority>{$priority}</priority>"; $xml_contents_post .= "</url>"; } } ## 获取分类 $category_ids = get_all_category_ids(); foreach ($category_ids as $cat_id) { $loc = get_category_link($cat_id); $lastmod = date("Y-m-d\\TH:i:s+00:00", current_time('timestamp', '1')); $changefreq = 'Weekly'; $priority = '0.3'; $xml_contents_cat .= "<url>"; $xml_contents_cat .= "<loc>{$loc}</loc>"; $xml_contents_cat .= "<lastmod>{$lastmod}</lastmod>"; $xml_contents_cat .= "<changefreq>{$changefreq}</changefreq>"; $xml_contents_cat .= "<priority>{$priority}</priority>"; $xml_contents_cat .= "</url>"; } $xml_contents = $xml_contents_post . $xml_contents_page . $xml_contents_cat; } ##HTML生成 $post = query_posts('ignore_sticky_posts=1&posts_per_page=1000'); while (have_posts()) { the_post(); $html_contents .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '" target="_blank">' . get_the_title() . '</a></li>'; } if ($html_contents) { list($today_year, $today_month, $today_day, $hour, $minute, $second) = split('([^0-9])', current_time('mysql')); $updated_time = "{$today_year}-{$today_month}-{$today_day} {$hour}:{$minute}:{$second}"; $html = ' <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>' . $blog_name . ' 站点地图HTML文件</title> <meta name="keywords" content="' . $blog_keywords . '" /> <meta name="generator" content="这里是' . $blog_name . '的HTML站点地图,相信有了它之后,你一定能对唯爱网有一个更全面的认识!" /> <link rel="author" href="https://www.weilove.me/" /> <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="%blog_home%/feed"/> <style type="text/css"> * { margin: 0; padding: 0; } a { color: #444; } a:hover { color: #009AD9; } ul li { background: #eee; font-size: 13px; font-weight: normal; margin: 0px 30px; } body { font-family:microsoft yahei; font-size:13px; color: #444; line-height: 25px; background: whiteSmoke; } div#head{ background-color: #313131; padding: 12px 2%; color: #eee; font-size: 24px; } div#head a{ color:#eee; text-decoration: none; } div#content{ padding: 0px 2% 0px 2%; margin: 10px 0px 10px 0px; } div#content li { list-style: none; } div#content li.pagenav, li.categories, li.recentpost { font-size: 16px; font-weight: bold; } #footer { color: #ddd; background-color: #313131; padding: 12px 2%; } #footer a { color: #ddd; } </style> </head> <body> <div id="head"><a href="' . $blog_home . '" title="' . $blog_home . '|' . $blog_description . '">' . $blog_name . '</a>站点地图</a></div> <div id="content"> <li class="recentpost">所有文章</li> <ul>' . $html_contents . '</ul> ' . wp_list_categories('echo=0') . ' ' . wp_list_pages('echo=0') . ' </div> <div id="footer"> ©' . $blog_wp_year . ' <a href="' . $blog_home . '" title="' . $blog_home . '|' . $blog_description . '">' . $blog_name . '</a> | 此页面遵循HTML5规范,由 <a href="http://c8023.com/" title="唯爱网|记录生活,分享感动">唯爱网</a><a target="_blank" href="http://c8023.com/" title="唯爱网"></a> 自动生成 | 最后更新:' . $updated_time . ' </div> </body> </html>'; $GetHomePath = GetHomePath(); $filename_html = $GetHomePath . 'sitemap.html'; if (IsFileWritable($GetHomePath) || IsFileWritable($filename_html)) { file_put_contents("{$filename_html}", "{$html}"); @chmod($filename_html, 0777); } else { echo '抱歉,站点地图HTML文件生成失败!请确保站点根目录及sitemap.html具有写权限。.'; } } if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('do_sitemap_by_post'); } }