我正在编写一些 php 来查询我的 wordpress 博客数据库并在我的主页上显示最新的帖子,该帖子在 wordpress 环境之外。
我不是很精通php,但我已经能够显示最新的博客标题以及帖子内容。我想做的是让缩略图成为帖子的可点击链接。我如何获得帖子的链接?我还想只显示摘录而不是整个帖子,但是以与 post_title、post_content 相同的方式使用 post_excerpt 似乎不起作用。
// ...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
多谢你们