幫客戶開發(fā)wordpress站點時經(jīng)常會遇到各種要求,這次幫一個客戶開發(fā)項目時,,客戶要求幫他開發(fā)的站點的文章能在其他網(wǎng)站調(diào)用,,并且要以HTML的形式來調(diào)用不能使用js,說是做鏈輪什么的,。沒辦法顧客就是上帝,,繼續(xù)折騰唄。下面來說下實現(xiàn)方法,,首先在wordpress的根目錄新建一個html_post.php文件,,記住是需要向外調(diào)用文章的wordpress站點。html_post.php文件的代碼如下:
<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');query_posts('showposts=10&orderby=new');?><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; ?>
這樣就可以調(diào)用網(wǎng)站中最新的10篇文章了,,showposts=10這個數(shù)字可以修改成你想要調(diào)用文章的數(shù)量,。下面我來給大家仔細(xì)講解下如何來修改代碼達(dá)到調(diào)用自己想要調(diào)用文章的效果。
1,、如果我想要調(diào)用某個分類的下的最新文章該如何實現(xiàn)呢,?
其實這點很容易實現(xiàn)的只需要修改下query_posts這個參數(shù),比如我指定要調(diào)用的分類的ID是1那么代碼就變成了:
<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');query_posts('showposts=10&orderby=new&cat=1');?><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,、如果我想調(diào)用全站但只屏蔽某個分類下的文章呢,?
<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');query_posts('showposts=10&orderby=new&cat=-1');?><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、如果我想調(diào)用隨機(jī)文章呢,?
<?phpdefine('WP_USE_THEMES', false);require('./wp-load.php');query_posts('showposts=10&orderby=rang');?><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');query_posts('showposts=10&orderby=rang');?><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; ?>
下面站外來調(diào)用的方法:
<?php//該代碼放置在需要調(diào)用文章內(nèi)容和列表的地方$url='http://你的站點地址/html_post.php';echo file_get_contents( $url );?>
大功告成。上面介紹的方法都必須要在調(diào)用站點支持php的情況下才可行,,如果調(diào)用站點支持asp的話只要把讀取html_post.php的PHP代碼用ASP重寫一遍,,但是如果是靜態(tài)空間就只能用js來調(diào)用咯。