Wordpress does not have a plug-in to generate a TXT website map

Source: Internet
Author: User
Tags foreach php file php code xmlns

This method does not require any plug-ins to be installed and is generated in pure code.

<? Phprequire ('. /wp-blog-header.php '); header ('content-type: application/txt'); header ('http/1.1 200 OK '); $ posts_to_show = 50000; // limit the maximum number of articles?> <? Phpheader ("Content-type: text/txt"); $ myposts = get_posts ("numberposts =". $ posts_to_show); foreach ($ myposts as $ post) {?> <? Php the_permalink ();?> <? Php echo "\ n";?> <? Php }?>



Copy and save the above code as a php file. Note that the UTF-8 format is used. Then upload it to your wordpress installation root directory.

Note: change www.admin.com to your website address.

Set pseudo-static

① Nginx
Edit an existing Nginx pseudo-static rule. Add the following rule and restart nginx:

Rewrite ^/ping.txt $/ping. php last;

② Apache
Edit. htaccess in the root directory of the website and add the following rules:

RewriteRule ^ (ping) \. xml $ 1.php

After pseudo-static rules are completed, you can directly access sitemap. xml to see the effect.

Finally, enter http://www.admin.com/ping.txtto see the effect of the TXT format website map generated by the wordpressno plug-in pure code. To download the txt file, right-click it and choose Save!


WordPress plug-in-free php code for generating a complete site map (sitemap. xml)

<? Phprequire ('. /wp-blog-header.php '); header ("Content-type: text/xml"); header ('http/1.1 200 OK'); $ posts_to_show = 1000; echo '<? Xml version = "1.0" encoding = "UTF-8"?> '; Echo' <urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" xmlns: mobile = "http://www.baidu.com/schemas/sitemap-mobile/1/"> '?> <! -- Generated-on = <? Php echo get_lastpostdate ('blog ');?> --> <Url> <loc> <? Php echo get_home_url ();?> </Loc> <lastmod> <? Php $ ltime = get_lastpostmodified (GMT); $ ltime = gmdate ('Y-m-d \ TH: I: s + 00:00 ', strtotime ($ ltime )); echo $ ltime;?> </Lastmod> <changefreq> daily </changefreq> <priority> 1.0 </priority> </url> <? Php/* article page */header ("Content-type: text/xml"); $ myposts = get_posts ("numberposts = ". $ posts_to_show); foreach ($ myposts as $ post) {?> <Url> <loc> <? Php the_permalink ();?> </Loc> <lastmod> <? Php the_time ('C')?> </Lastmod> <changefreq> monthly </changefreq> <priority> 0.6 </priority> </url> <? Php}/* end of the article loop */?> <? Php/* single page */$ mypages = get_pages (); if (count ($ mypages)> 0) {foreach ($ mypages as $ page) {?> <Url> <loc> <? Php echo get_page_link ($ page-> ID);?> </Loc> <lastmod> <? Php echo str_replace ("", "T", get_page ($ page-> ID)-> post_modified);?> + </Lastmod> <changefreq> weekly </changefreq> <priority> 0.6 </priority> </url> <? Php }}/ * the cycle of a single page ends */?> <? Php/* blog classification */$ terms = get_terms ('Category ', 'orderby = name & hide_empty = 0'); $ count = count ($ terms ); if ($ count> 0) {foreach ($ terms as $ term) {?> <Url> <loc> <? Php echo get_term_link ($ term, $ term-> slug);?> </Loc> <changefreq> weekly </changefreq> <priority> 0.8 </priority> </url> <? Php }}/ * classification cycle ends */?> <? Php/* tag (optional) */$ tags = get_terms ("post_tag"); foreach ($ tags as $ key => $ tag) {$ link = get_term_link (intval ($ tag-> term_id), "post_tag"); if (is_wp_error ($ link) return false; $ tags [$ key]-> link = $ link;?> <Url> <loc> <? Php echo $ link?> </Loc> <changefreq> monthly </changefreq> <priority> 0.4 </priority> </url> <? Php}/* tag loop ends */?> </Urlset>



Wordpress non-plug-ins implement website maps in xml format

<? Phprequire ('. /wp-blog-header.php '); header ("Content-type: text/xml"); header ('http/1.1 200 OK'); $ posts_to_show = 1000; // Obtain the number of articles echo '<? Xml version = "1.0" encoding = "UTF-8"?> '; Echo' <urlset xmlns: xsi = "<a href =" http://www.w3.org/2001/XMLSchema-instance "rel =" external nofollow "> http://www.w3.org/2001/XMLSchema-instance </a>" xmlns = "<a href =" http://www.sitemaps.org/schemas/sitemap/0.9 "rel =" external nofollow "rel = "external nofollow"> http://www.sitemaps.org/schemas/sitemap/0.9 </a> "xsi: schemaLocation = "<a href =" http://www.sitemaps.org/schemas/sitemap/0.9 "rel =" external nofo Llow "rel =" external nofollow "> http://www.sitemaps.org/schemas/sitemap/0.9 </a> <a href =" http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd "> '" rel = "external nofollow"> http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd ">' </a>; ?> <! -- Generated-on = <? Php echo get_lastpostdate ('blog ');?> --> <Url> <loc> http: // localhost/</loc> <lastmod> <? Php echo get_lastpostdate ('blog ');?> </Lastmod> <changefreq> daily </changefreq> <priority> 1.0 </priority> </url> <? Phpheader ("Content-type: text/xml"); $ myposts = get_posts ("numberposts =". $ posts_to_show); foreach ($ myposts as $ post) {?> <Url> <loc> <? Php the_permalink ();?> </Loc> <lastmod> <? Php the_time ('C')?> </Lastmod> <changefreq> monthly </changefreq> <priority> 0.6 </priority> </url> <? Php} // end foreach?> </Urlset>



Copy the above code as the xmlmap. Php file and upload it to the website root directory.
Http: // localhost/xmlmap. php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.