wordpress站外以HTML的形式调用wordpress文章
帮客户开发wordpress站点时经常会遇到各种要求,这次帮一个客户开发项目时,客户要求帮他开发的站点的文章能在其他网站调用,并且要以HTML的形式来调用不能使用js,说是做链轮什么的。没办法顾客就是上帝,继续折腾呗。下面来说下实现方法,首先在wordpress的根目录新建一个html_post.php文件,记住是需要向外调用文章的wordpress站点。html_post.php文件的代码如下:
<?php define('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那么代码就变成了:
<?php define('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、如果我想调用全站但只屏蔽某个分类下的文章呢?
<?php define('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、如果我想调用随机文章呢?
<?php define('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、如果我想输出摘要呢?
<?php define('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 ); ?>
大功告成。上面介绍的方法都必须要在调用站点支持php的情况下才可行,如果调用站点支持asp的话只要把读取html_post.php的PHP代码用ASP重写一遍,但是如果是静态空间就只能用js来调用咯。
(资源库 www.zyku.net)
您可能感兴趣的文章
- 12-13ipadmini6如何进行分屏
- 10-27快手心情状态设置流程分享
- 11-12vivoX70Pro+丢了怎么找回
- 02-22DedeCMS发布留言时提示错误“数据校验
- 03-10帝国CMS添加百度地图标注的方法
- 01-10小米12Pro怎么设置24小时时间显示
- 01-14农场种植学习助手-农场种植学习助手应
- 10-18iwatch重新配对教程介绍
- 01-11大晟智慧家-大晟智慧家应用软件功能介
- 02-10Python使用protobuf序列化和反序列化
- 03-22Destoon URL Rewrite(伪静态)设置方法
- 12-14华为手机充电小精灵怎么关闭
- 01-12答题精灵-答题精灵应用软件功能介绍
- 02-13小米11指纹特效功能开启方法
- 12-23HTML <colgroup> 标签
- 01-11元和堂药业-元和堂药业应用软件功能介
- 03-24oppoa93应用隐藏功能设置方法
- 03-07华为麦芒10SE设置DC调光方法
- 05-07华为mate40pro皮套操作开启教程
- 04-10淘宝修改账号用户名操作方法
最近更新
阅读排行
猜你喜欢
- 03-21荣耀v9鸿蒙系统更新升级教程
- 09-22vivox70在哪里设置流量显示
- 01-11月光小说-月光小说应用软件功能介绍
- 02-18Oracle中instr和substr存储过程详解
- 10-12帝国cms网站安全设置 让你的网站更安
- 02-04opporeno5开启屏幕共享方法
- 03-30Sublime Text3-sublimerge 文本对比插
- 03-19红魔6触感功能关闭教程
- 07-31类似看云(KanCloud)的在线文档编辑平
- 10-14荣耀手机如何开启息屏显示时间