我使用这篇文章将我的博客文章拉到我的正常网站上。
https://wordpress.org/support/topic/display-posts-on-external-website
1) 以下是您要在 Doctype 之前编写的代码(因此是您的 HTML 的第一个):
<?php
//db parameters
$db_username = '###';
$db_password = '###';
$db_database = '###';
$blog_url = 'http://www.jamischarles.com/blog/'; //base folder for the blog. Make SURE there is a slash at the end
//connect to the database
mysql_connect(localhost, $db_username, $db_password);
@mysql_select_db($db_database) or die("Unable to select database");
//get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 5";
$query_result = mysql_query($query);
$num_rows = mysql_numrows($query_result);
//close database connection
mysql_close();
// html page starts after ?>
?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
2) 现在,正文中的文本会有些不同。继续我们离开的地方……
现在,这里的问题是我们有动态生成的内容。这意味着我们编写了一个循环,遍历数据库中的每个表行,获取标题、日期和文本,然后输出 html,然后转到数据库的下一行并再次执行相同的操作。
因此,如果我们使用具有相同 id 的 div,它将显示该 div 5 次,每次显示不同的帖子。这是不可接受的,因为它不是有效代码,并且可能会弄乱 CSS。所以我们必须给它一个有效的类,或者使用表。在本例中,我们将使用 div。
<body>
<?php
//start a loop that starts $i at 0, and make increase until it's at the number of rows
for($i=0; $i< $num_rows; $i++){
//assign data to variables, $i is the row number, which increases with each run of the loop
$blog_date = mysql_result($query_result, $i, "post_date");
$blog_title = mysql_result($query_result, $i, "post_title");
$blog_content = mysql_result($query_result, $i, "post_content");
//$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.
$blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format
//format date
$blog_date = strtotime($blog_date);
$blog_date = strftime("%b %e", $blog_date);
//the following HTML content will be generated on the page as many times as the loop runs. In this case 5.
?>
</body>
<div class="post"></div>
<span class="date"> <?php echo $blog_date; ?>:</code></span><br /><hr />
<a href="http://www.bluebreeze.net/blog"><?php echo $blog_title; ?></a> <br /><br />
<?php echo $blog_content; ?> <br /><br />
<a href=”<?php echo $blog_permalink; ?>”>This Article</a> <br />
<a href="http://www.bluebreeze.net/blog">More Articles </a>
<?php
} //end the for loop
?>