看来,切换到博客可能太不可预测,无法用于主要网站设计。这是我第一次尝试基于SQL的解决方案。
function get_intro_post($blogid, $thumb_size=\'\') {
global $wpdb, $post;
// Get the system defined table prefix
$prefix = $wpdb->prefix;
// Create a full table prefix by combining the system prefix with the blogid
$tbl = $prefix . $blogid;
// First query selects posts with the \'Introduction\' category
$sql = "SELECT `ID` FROM {$tbl}_posts as p";
$sql .= " JOIN {$tbl}_term_relationships tr ON p.ID = tr.object_id";
$sql .= " JOIN {$tbl}_terms t ON tr.term_taxonomy_id = t.term_id";
$sql .= " WHERE t.name = \'Introduction\'";
// Only want/expect one result row, so use get_row()
$intro = $wpdb->get_row($sql);
$postid = $intro->ID;
// Second query joins the postmeta table to itself so that I can find
// the row whose post_id matches the _thumbnail_id, found in meta_value,
// for the post whose post_id matches the one I found in the previous query.
$sql = "SELECT p2.meta_key, p2.meta_value FROM `{$tbl}_postmeta` p1";
$sql .= " JOIN `{$tbl}_postmeta` p2 ON p1.meta_value = p2.post_id";
$sql .= " WHERE p1.meta_key = \'_thumbnail_id\'";
$sql .= " AND p1.post_id = \'{$postid}\'";
$sql .= " LIMIT 0 , 30";
// Expecting 2 result rows, so use get_results()
$thumb_meta = $wpdb->get_results($sql);
// Set $src to FALSE as the default
$src = FALSE;
// Make sure the query returned results
if(!empty($thumb_meta)) {
foreach($thumb_meta as $row) {
// We just want to find the row where meta_key is the attached file
// and then set our $src var to that value, which is the image path
if ($row->meta_key == "_wp_attached_file") $src = $row->meta_value;
}
}
// If we found an image path above, I\'ll append the rest of the path to the front
if($src) { $src = "/wp-content/blogs.dir/{$blogid}/files/{$src}"; }
// Handy core function to grab the blogname of another network\'s blog, by ID
$blogname = get_blog_details( $blogid )->blogname;
// Make an associative array holding the elements we need,
// some pulled from the global $post context
$p = array(
\'title\' => get_the_title(),
\'content\' => get_the_content(),
\'excerpt\' => get_the_excerpt(),
\'permalink\' => get_permalink(),
\'blogname\' => $blogname,
\'thumbnail\' => $src
);
// Return an object with the post details mentioned above
return (object) $p;
}