Firstly, sorry if this has been dealt with before, I've spent quite a while searching posts to no avail.
I have a Wordpress blog and a Concrete5 site. I am trying to write out the three most recent Wordpress blog posts on the Concrete5 site. I cannot use RSS as both sites are on the same server and internal RSS is disabled (is there a way to get around this?).
I have written a block for concrete and have put this code in view.php ...
<?php
define('WP_USE_THEMES', false);
require('path-to-wordpress/wp-blog-header.php');
?>
... This results in "Error establishing a database connection".
If I run this outside of Concrete it works fine (I'm currently using this code elsewhere on the server, no probe).
I've also tried with wp_load.php, same result.
Sorry if this is really obvious, I've been working on it for a while now :(
Thanks in advance.
Sadly this simple approach to loading Wordpress does not work in concrete for whatever reason, some kind of conflicting definition or something. If you are trying to load Wordpress posts on a Concrete5 site on the same server you might well find that using RSS will not work for you as sometimes servers block internal requests to prevent looping.
This is the position I found myself in so I decided to access the Wordpress table myself building on code posted by 'jcrens8392' on the Concrete5 forums , so here it is for anyone who finds them self in the same position.
// 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 );
}
I put these functions in controller.php (excerpt function from 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;
}
This really isn't the ideal solution, it does, however, have the distinct advantage of working. Hope this helps someone.