加载2个不同的wp-load

时间:2018-03-21 16:39:38

标签: php wordpress

我有2个不同数据库的WordPress安装但在同一台服务器上。我想在一个外部首页上显示来自我的两个博客的最新帖子和图片库,我不能使用多站点或RSS或wp API JSON来显示它。所以我试着像这样加载2个wp-load.php

function showcontentblog1 () {

require_once("/home/user/public_html/domain/wp-load.php");

$wp_query = new \WP_Query();
    $args=array(
      'tag' => 'video',
      'showposts'=>3,
      'caller_get_posts'=>3
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      //echo '5 recent Posts with tag';
      while ($my_query->have_posts()) : $my_query->the_post();



    $pid = get_the_ID();
$link = get_permalink();
    $post = get_post( $pid );
    $content = $post->post_content;


$doc = new DOMDocument();
$doc->loadHTML($content);
$imageTags = $doc->getElementsByTagName('img');








foreach($imageTags as $tag) {



echo '<a href=' .'"' .$link .'"' .'/>' .'<img src="' .$tag->getAttribute('src') .'"' .'" alt="'.$post->post_title .'">' .'</a>';


}



      endwhile;
    } //if ($my_query)

}




function showcontentblog2 () {

require_once("/home/user/public_html/domain/domain2/wp-load.php");

$wp_query = new \WP_Query();
    $args=array(
      'tag' => 'gallery',
      'showposts'=>3,
      'caller_get_posts'=>3
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      //echo '5 recent Posts with tag';
      while ($my_query->have_posts()) : $my_query->the_post();



    $pid = get_the_ID();
$link = get_permalink();
    $post = get_post( $pid );
    $content = $post->post_content;


$doc = new DOMDocument();
$doc->loadHTML($content);
$imageTags = $doc->getElementsByTagName('img');








foreach($imageTags as $tag) {



echo '<a href=' .'"' .$link .'"' .'/>' .'<img src="' .$tag->getAttribute('src') .'"' .'" alt="'.$post->post_title .'">' .'</a>';


}



      endwhile;
    } //if ($my_query)

}

 echo showcontentblog1();

 echo showcontentblog2();

但它只加载函数showcontentblog1,showcontentblog2 not load

提前致谢

1 个答案:

答案 0 :(得分:0)

这不起作用,因为wp-load.php定义了常量ABSPATH,如下所示:

if ( ! defined( 'ABSPATH' ) ) {
  define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}

由于PHP常量在执行脚本期间无法更改,因此第二个wp-load.php将不会使用其ABSPATH,而是使用第一个wp-load.php的ABSPATH。由于使用的wp-config.php文件由ABSPATH确定,第二个wp-load.php将使用第一个wp-load.php的wp-config.php。

此问题可能还有许多其他安装特定常量。 WordPress安装最初设置要运行的环境。您尝试首先在一个环境中运行代码,然后在第二个环境中运行代码。我不认为可以这样做,因为第一个环境无法清除,第二个环境无法取代第一个环境。

相关问题