欢迎来到资源库(www.zyku.net)

WordPress

当前位置:首页 > CMS教程 > WordPress > wordpress文章调用

wordpress站外以HTML的形式调用wordpress文章

时间:2017-03-04|栏目: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)

上一篇:wordpress优化wp_nav_menu函数

栏    目:WordPress

下一篇:wordpress自定义系统默认发信人的方法

本文标题:wordpress站外以HTML的形式调用wordpress文章

本文地址:https://www.zyku.net/wp/1070.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务