至少有two (2) ways 要实现这一点:
你可以query the second database 这将要求您在两个位置维护数据库凭据,或者至少在包含文件中维护数据库凭据,或者
你可以create a simple JSON feed 对于你的照片博客,使用主博客上的提要并将其缓存一段时间。当缓存过期时,这将导致页面加载稍有延迟,但由于两者都在同一台机器上,因此不应成为问题。
请告诉我您喜欢哪一种,以及我是否会更新答案,以便您如何更新。
更新要连接到另一个数据库,您可以在此处阅读有关它的更多信息:
这是我为您拼凑的一个函数,名为
show_thumbnails_2nd_db()
可以复制到主题的
functions.php
文件:
function show_thumbnails_2nd_db() {
global $wpdb;
global $table_prefix;
$save_wpdb = $wpdb;
$save_prefix = $table_prefix;
include_once(ABSPATH . \'/photos/database-credentials.php\');
extract($database_credentials);
$wpdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
wp_set_wpdb_vars();
// This is the code for featured images
$sql = <<<SQL
SELECT DISTINCT CONCAT(\'<img src="\',attachment.guid,\'"/>\') AS url
FROM
{$wpdb->posts} attachment
INNER JOIN {$wpdb->postmeta} ON attachment.ID={$wpdb->postmeta}.meta_value AND {$wpdb->postmeta}.meta_key=\'_thumbnail_id\'
INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id
WHERE {$wpdb->posts}.post_status=\'publish\'
AND attachment.post_type=\'attachment\'
ORDER BY attachment.ID DESC
LIMIT 10
SQL;
$post_urls = $wpdb->get_col($sql);
if (is_array($post_urls))
echo implode("\\n",$post_urls);
// This is the code for post thumbnails
// $sql = <<<SQL
// SELECT DISTINCT {$wpdb->posts}.ID
// FROM {$wpdb->posts}
// INNER JOIN {$wpdb->posts} attachment
// ON {$wpdb->posts}.ID=attachment.post_parent
// WHERE {$wpdb->posts}.post_status=\'publish\'
// AND attachment.post_type=\'attachment\'
// ORDER BY attachment.ID
// LIMIT 10
// SQL;
// $post_ids = $wpdb->get_col($sql);
// foreach($post_ids as $post_id) {
// $thumbnail = get_the_post_thumbnail($post_id);
// if ($thumbnail) {
// echo $thumbnail;
// }
// }
$table_prefix = $save_prefix;
$wpdb = $save_wpdb;
}
Note: 以上假设您已创建
database-credentials.php
在照片博客的根目录中,应该是这样的:
<?php
$database_credentials = array(
\'DB_NAME\' => \'your_database\',
\'DB_USER\' => \'your_db_user\',
\'DB_PASSWORD\' => \'your db password\',
\'DB_HOST\' => \'localhost\',
\'table_prefix\' => \'your_photos_db_prefix_\',
);
那么
inside the /wp-config.php
for your photos blog 您将替换如下代码部分:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', \'your_database\');
/** MySQL database username */
define(\'DB_USER\', \'your_db_user\');
/** MySQL database password */
define(\'DB_PASSWORD\', \'your db password\');
/** MySQL hostname */
define(\'DB_HOST\', \'localhost\');
还有这个:
$table_prefix = \'wp_\';
使用此选项:
include_once(ABSPATH . \'/database-credentials.php\');
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', $database_credentials[\'DB_NAME\']);
/** MySQL database username */
define(\'DB_USER\', $database_credentials[\'DB_USER\']);
/** MySQL database password */
define(\'DB_PASSWORD\', $database_credentials[\'DB_PASSWORD\']);
/** MySQL hostname */
define(\'DB_HOST\', $database_credentials[\'DB_HOST\']);
$table_prefix = $database_credentials[\'table_prefix\'];
如果你需要更多的解释,请在评论中提问。