我正在编写一些php来查询我的wordpress博客数据库,并在我的主页上显示wordpress环境之外的最新帖子 .
我不是很精通php,但我能够显示最新的博客 Headers ,以及帖子内容 . 我想做的是让缩略图成为帖子的可点击链接 . 我如何获得帖子的链接?我也想显示摘录而不是整个帖子 - 但是使用post_excerpt以与post_title相同的方式,post_content,似乎不起作用 .
// ...Connect to WP database
$dbc = mysql_connect(XXX,XXX,XXX);
if ( !$dbc ) {
die( 'Not Connected: ' . mysql_error());
}
// Select the database
$db = mysql_select_db('wrd_2ikhd5ho53');
if (!$db) {
echo "There is no database: " . $db;
}
// ...Formulate the query
$query = "
SELECT post_title,post_content,UNIX_TIMESTAMP(post_date) AS post_date_unix, ID
FROM `wp_posts`
WHERE `post_status` = 'publish'
AND `post_password` = ' '
AND `post_type` = 'post'
ORDER BY `wp_posts`.`post_date` DESC
";
// ...Perform the query
$result = mysql_query( $query );
// ...Check results of the query and terminate the script if invalid results
if ( !$result ) {
$message = '<p>Invalid query.</p>' . "\n";
$message .= '<p>Whole query: ' . $query ."</p> \n";
die ( $message );
}
// Init a variable for the number of rows of results
$num_rows = mysql_num_rows( $result );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
// Init var for DATE of the post
$post_date = date( "l, F jS, Y ", $row['post_date_unix'] );
// Init var for TITLE of the post
$post_title = $row['post_title'];
// Init var for CONTENT of the post
$post_content = $row['post_content'];
$post_excerpt = $row['post_excerpt'];
// Init var for Excerpt of the post
// Print the number of posts
echo "$post_title";
echo "$post_date";
// Free the resources associated with the result set
if ( $result ) {
mysql_free_result( $result );
mysql_close();
}
?>
要引用的网站是http://www.uniconutrition.com
多谢你们
2 回答
它可以比使用 WordPress database layer 尝试的更容易 . 见http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
基本上:
或者, there is always RSS 从WordPress中获取一个项目, Headers 和摘录 .
Developer's Guide - Google AJAX Feed API - Google Code
就摘录而言,您不是在查询中使用它 . 将查询的开头更改为:
至于帖子的链接,我不是100%确定你可以在不通过WordPress机器的情况下获得"pretty-printable"链接 .
wp_posts
表中的guid
列下提供了一个短链接样式的URL,但WP文档声称如果禁用了非常永久链接,它可能不会显示 . 由于你知道帖子的ID(它是你查询的一部分),你可以使用WordPress函数来获取链接 . 有关更多信息,请参见get_permalink文档页面 .另外,请查看PDO或Mysqli作为
mysql_*
函数的替代 . 你不应该再使用后者了 .