^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

WordPress调用另一个站点最新文章列表

看到大前端文章页标签下面调用了阿里百秀的 4 篇最新文章,有日期,有缩略图,有文章标题,陌小雨觉得很不错,据说这是广告,应该要给钱的吧,^_^。陌小雨现在有两个站点,总希望两者之间能有点联系,所以就想在陌小雨(以下简称 A 站)文章页调用体操之乡(以下简称 B 站)的最新文章列表,也就是 wordpress 实现站外调用。

陌小雨原先的想法是在 B 站新建一个模板调用最新的 4 篇文章,然后在 B 站新建一个页面,选中刚刚新建的模板,再在 A 站通过自带的广告位的 js 调用实现。具体方法和代码如下:

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

1、B 站的模板文件 new.php

<?php
/*template name: 最新文章*/
?>
  <style>
  .pads {
    overflow: hidden;
    padding: 0px;
    margin: 0px -2% 10px 0px;
}
.pads li {
    width: 23%;
    display: inline-block;
    vertical-align: top;
    margin-bottom: 20px;
    margin-right: 1.5%;
}
.pads a {
    display: block;
    position: relative;
}
.pads img {
    max-width: 100%;
    margin-bottom: 5px;
}
.pads h4, a {
    transition: all 0.25s ease 0s;
}
.pads h4 {
    font-weight: bold;
    font-size: 14px;
    line-height: 18px;
    color: #666;
    margin: 0px;
}
.pads time {
    color: #FFF;
    background-color: #FF5E52;
    display: inline-block;
    padding: 2px 5px;
    font-size: 12px;
    position: absolute;
    top: 0px;
    left: 0px;
}
  </style>
  <ul class="pads">
  <?php $post_query = new WP_Query('showposts=4');while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
  <li><a href="<?php the_permalink(); ?>" target="_blank"><?php echo post_thumbnail_img(240,180);?><h4><?php the_title(); ?></h4><time><?php the_time('m-d')?></time></a></li>
  <?php endwhile;?>
  </ul>

2、js 调用代码

<script type="text/javascript">document.write('<iframe width="100%"  height="245px"  frameborder="0" scrolling="no" border="0" src="https://dedewp.com/new" style="width: 100%;"></iframe>');</script>

现在陌小雨用的是另外一种办法:在 B 站根目录新建 new.php 文件,然后在 A 站通过 php 代码调用

1、new.php 文件代码

<?php
define('WP_USE_THEMES', false);
require('./wp-load.php');
query_posts('showposts=4&orderby=new');
?>
<style>
  .pads {
    overflow: hidden;
    padding: 0px;
    margin: 0px -2% 10px 0px;
}
.pads li {
    width: 23%;
    display: inline-block;
    vertical-align: top;
    margin-bottom: 20px;
    margin-right: 1.2%;
}
.pads a {
    display: block;
    position: relative;
    text-decoration: none;}
.pads img {
    max-width: 100%;
    margin-bottom: 5px;
}

.pads h4, a {
    transition: all 0.25s ease 0s;
}
.pads h4 {
    font-weight: bold;
    font-size: 14px;
    line-height: 18px;
    color: #666;
    margin: 0px;
}
.pads time {
    color: #FFF;
    background-color: #FF5E52;
    display: inline-block;
    padding: 2px 5px;
    font-size: 12px;
    position: absolute;
    top: 0px;
    left: 0px;
}
</style>
<ul class="pads">
<?php while (have_posts()): the_post(); ?>
  <li><a href="<?php the_permalink(); ?>" target="_blank"><?php echo post_thumbnail_img(240,180);?><h4><?php the_title();?></h4><time><?php the_time('m-d');?></time></a></li>
  <?php endwhile;?>
  </ul>

2、php 调用

<?php
$url='http://你的站点地址/new.php';
echo file_get_contents( $url );
?>

两种办法各有利弊,第二种输出的代码更简洁,但需要将 php 代码插入到模板文件中,第一种办法可以在后台很方便的插入到广告位,请自行选用。有点强迫症的我还是选择了第二种方式。

上面分享的是调用 B 站最新的四篇文章,如果是调用指定分类或者排除某分类或者是随机输出只需要修改下第四行 query_posts 这个地方就可以了。

比如要调用指定分类(分类 ID 是 1)那么需修改为:

/** 如果想同时调用多个分类用半角符分隔如 cat=1,2,3,4 */
query_posts('showposts=10&orderby=new&cat=1,2,3,4');

如果想调用全站文章但只屏蔽某个分类下的文章就需修改为:

/** 如果想同时屏蔽多个分类用半角符分隔如 cat=-1,-2,-3,-4 */
query_posts('showposts=10&orderby=new&cat=-1,-2');

如果想调用随机文章,代码需修改为“

query_posts('showposts=10&orderby=rand');

这种通过 WordPress 调用另一个站点最新文章列表来获取广告费的办法对流量大的站点陌小雨觉得是个不错的思路哦,你也想试试了么?

赠人玫瑰,手有余香。