我有两个完全独立的WP网站设置。不同的域,不同的数据库。我管理它们,它们都托管在专用服务器上。我试图包含一些基本内容,这些内容只需要比RSS提要多一点。我需要从SITE-1中提取数据并在SITE-2上显示,使用循环中的基本WP格式。我看遍了所有地方,这似乎都是不可能的。我试着打电话给wp load。php,但无法使其工作,甚至不确定这是否是正确的方式。我可以访问这两个站点的根服务器,必要时甚至可以访问服务器根。有什么办法吗?谢谢
在单独的WP网站上显示来自一个WP网站的内容
2 个回复
最合适的回答,由SO网友:onetrickpony 整理而成
是 啊
$wpdb2 = new wpdb(\'dbuser\', \'dbpassword\', \'dbname\', \'dbhost\');
// get 10 posts, assuming the other WordPress db table prefix is "wp_"
$query = "SELECT post_title, guid FROM wp_posts
WHERE post_status = \'publish\'
AND post_type = \'post\'
ORDER BY post_date DESC LIMIT 10";
$someposts = $wpdb2->get_results($query, OBJECT);
foreach($someposts as $somepost)
echo "<a href=\\"{$somepost->guid}\\">{$somepost->post_title}</a><br />";
另一种方法是使用HTTP api:要在其中显示数据的第一个站点中的代码:
$send = array(
\'body\' => array(
\'action\' => \'get_some_posts\',
// send other data here, maybe a user/password if you\'re querying senstive data
),
\'user-agent\' => \'RodeoRamsey; \'.get_bloginfo(\'url\')
);
$response = wp_remote_post(\'http://yoursiteurl.com/\', $send);
if (!is_wp_error($response) && ($response[\'response\'][\'code\'] == 200)) echo $response[\'body\'];
在第二个站点的主题函数中编写代码。php(或创建插件):add_action(\'template_redirect\', \'process_post_request\');
function process_post_request(){
if($_POST[\'action\'] == \'get_some_posts\'):
$posts = new WP_Query();
$query = array(\'posts_per_page\' => 10);
$posts->query($query);
while ($posts->have_posts()):
$posts->the_post(); // here\'s the usual loop
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
<?php
endwhile;
die();
endif;
}
从“格式化”的角度来看,第二种方法更简单、更灵活。例如,在这里,您可以轻松地将帖子缩略图作为html进行回显,而使用数据库方法,您将很难获得指向缩略图图像的链接。。。SO网友:MikeSchinkel
我认为你的问题与这个问题非常相似:
- Getting post-thumbnails from another WP site$wpdb 使用其他站点的安全凭据。看看我对这个问题的回答,告诉我它是否回答了你的问题,如果没有,为什么我可以提供更好的答案。
结束