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