小編在幫客戶開發 WordPress
點時經常會遇到各種要求,這次幫一個客戶開發專案時,客戶要求幫他開發的站點的文章能在其他網站呼叫,並且要以 HTML 的形式來呼叫不能使用 js,說是做
鏈輪什麼的。沒辦法顧客就是上帝,繼續折騰唄。下面來說下實現方法,首先在 WordPress 的根目錄新建一個 html_post.php 檔案,記住是需
要向外呼叫文章的 WordPress 站點。 html_post.php 檔案的程式碼如下:

<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');query_posts('showposts=10&orderby=new');?>/** charset=UTF-8"防止亂碼 */<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <?php while (have_posts()): the_post(); ?><li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li><?php endwhile; ?>

這樣就可以呼叫網站中最新的 10 篇文章了,showposts=10 這個數字可以修改成你想要呼叫文章的數量。下面我來給大家仔細講解下如何來修改程式碼達到呼叫自己想要呼叫文章的效果。

1 、如果我想要呼叫某個分類的下的最新文章該如何實現呢?

其實這點很容易實現的只需要修改下 query_posts 這個引數,比如我指定要呼叫的分類的 ID 是 1 那麼程式碼就變成了:

<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');/** 如果想同時呼叫多個分類用半形符分隔如 cat=1,2,3,4 */query_posts('showposts=10&orderby=new&cat=1');?>/** charset=UTF-8"防止亂碼 */<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><?php while (have_posts()): the_post(); ?><li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li><?php endwhile; ?>

2 、如果我想呼叫全站站問斬但只遮蔽某個分類下的文章呢?

<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');/** 如果想同時遮蔽多個分類用半形符分隔如 cat=-1,-2,-3,-4 */query_posts('showposts=10&orderby=new&cat=-1');?>/** charset=UTF-8"防止亂碼 */<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><?php while (have_posts()): the_post(); ?><li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li><?php endwhile; ?>

3 、如果我想呼叫隨機文章呢?

<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');/** 如果想同時遮蔽多個分類用半形符分隔如 cat=-1,-2,-3,-4 */query_posts('showposts=10&orderby=rang');?>/** charset=UTF-8"防止亂碼 */<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><?php while (have_posts()): the_post(); ?><li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li><?php endwhile; ?>

4 、如果我想輸出摘要呢?

<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');/** 如果想同時遮蔽多個分類用半形符分隔如 cat=-1,-2,-3,-4 */query_posts('showposts=10&orderby=rang');?>/** charset=UTF-8"防止亂碼 */<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><?php while (have_posts()): the_post(); ?><li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"..."'utf-8'); ?></li><?php endwhile; ?>

差點都忘了~下面小編就再來說說站外如何來呼叫~

<?php//該程式碼放置在需要呼叫文章內容和列表的地方$url='http://你的站點地址/html_post.php';echo file_get_contents( $url );?>

OK
大功告成。調出來的文章都是純 HTML 的~不是什麼 js 噢,對 SEO 非常友好。另外小編提示下:上面介紹的方法都必須要在呼叫站點支援 php 的情況下才可
行,如果呼叫站點支援 asp 的話只要把讀取 html_post.php 的 PHP 程式碼用 ASP 重寫一遍,但是如果是靜態空間就只能用 js 來呼叫咯。