可悲的是這種簡單的方法來加載的WordPress不起作用在如果你試圖在同一臺服務器上的Concrete5站點上加載Wordpress的帖子,你可能會發現使用RSS不會對你有用,因爲有時服務器會阻止內部請求以防止循環。
這是我發現自己的位置,所以我決定訪問Wordpress表格,自己在Concrete5 forums上'jcrens8392'發佈的代碼上構建,因此這裏適用於任何發現它們都處於相同位置的人。
// posts and stuff
$category = 3
$items = 4
// wordpress db stuff
$dbUser = 'user';
$dbPass = 'password';
$dbHost = 'localhost';
$dbName = 'name';
// connect to wordpress database
$conn = mysql_connect($dbHost, $dbUser, $dbPass);
// handle connection errors
if(!$conn) {
$aDebug['Unable to connect to DB'] = mysql_error();
} elseif(!mysql_select_db($dbName, $conn)) {
$aDebug['Unable to select database'] = mysql_error();
}
// make SQL query
else {
$sQuery = "SELECT wp_posts.post_date, wp_posts.post_content , wp_posts.guid , wp_posts.post_title, wp_posts.post_excerpt, wp_posts.post_name FROM wp_posts , wp_term_relationships WHERE post_type = 'post' AND post_status = 'publish' AND wp_posts.ID = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = ".$category." ORDER BY post_date DESC LIMIT ".$items;
$rPosts = mysql_query($sQuery, $conn);
}
// plonk posts into a convinient array
$posts = array();
while($row = mysql_fetch_array($rPosts)){
$excerpt = $controller->getExcerpt(utf8_encode(strip_tags($row['post_content'])) , 0 , 200) .' <a href="'.$row['guid'].'">Read More →</a>';
$date = $controller->simplifyDate($row['post_date']);
$temp = array ('post_title' => $row['post_title'] , 'post_excerpt' => $excerpt , 'post_date' => $date , 'post_link' => $row['guid']);
array_push($posts , $temp);
}
我把這些功能在Controller.php這樣(從phpsnaps摘錄功能)...
function getExcerpt($str, $startPos=0, $maxLength=100) {
if(strlen($str) > $maxLength) {
$excerpt = substr($str, $startPos, $maxLength-3);
$lastSpace = strrpos($excerpt, ' ');
$excerpt = substr($excerpt, 0, $lastSpace);
$excerpt .= '...';
} else {
$excerpt = $str;
}
return $excerpt;
}
function simplifyDate($str) {
$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
$strs = split (' ' , $str);
$date = split ('-' , $strs[0]);
$month = $months[ $date[1] - 1 ];
$day = $date[2];
$year = $date[0];
return $day . ' ' . $month . ' ' . $year;
}
這真的是不理想的解決方案,它但是,具有明顯的工作優勢。希望這可以幫助某人。