I have a WordPress website with blogs on it, but I also have a HTML/Dreamweaver site.
I'd like to have the WordPress blogs automatically display on my index.html
page when I update the WordPress website.
这是我在我的网站上用来从数据库中读取标题的示例:
<?php
//Database access
$dbname="db-blog";
$dbhost="localhost";
$dbuser="user";
$dbpass="secretpassword";
//SQL Befehl zur Abfrage der Postings
$sql = "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER by ID DESC LIMIT 0,6";
//Open database
$db = mysql_connect($dbhost,$dbuser,$dbpass) or die("no connection to database");
mysql_select_db($dbname, $db);
$sqlRes = mysql_query($sql,$db);
mysql_close($db);
//Output titles
$recordCount = mysql_num_rows($sqlRes);
for ($i = 0;$i < $recordCount;$i++) {
$arCur = mysql_fetch_array($sqlRes);
echo "<li><a href=\"" . $arCur["guid"] . "\" title=\"zur Seite\">" . $arCur["post_title"] . "</a></li>";
}
?>
如果需要,应该很容易适应也阅读内容。
如果您没有直接访问数据库的权限,以下脚本会从 RSS 提要访问博客项目。
<?php
//Settings
$blog_url = "http://blog.ekiwi.de"; //URL of blog without / at the end
$count = 5; //Number of posts that should be shown
//--------------------------------------------------------------------------------
$content = @file_get_contents($blog_url . "/?feed=rss2");
preg_match_all("/<item[ ]?.*>(.*)<\/item>/Uis", $content, $items);
$items = $items[1];
if(!empty($items)) {
if ($count > sizeof($items))
$count = sizeof($items);
for($i = 0; $i < $count; $i++) {
//read link
preg_match("/<link>(.*)<\/link>/Uis", $items[$i], $link);
//Read title
preg_match("/<title>(.*)<\/title>/Uis", $items[$i], $array_title);
$title = str_replace("<![CDATA[", "", $array_title[1]);
$title = str_replace("]]>", "", $title);
//Output title with link
echo "<li>\n";
echo " <a href=\"$link[1]\" title=\"zum Blog...\">$title</a>\n";
echo "</li>\n";
}
}
?>
两种解决方案都使用 PHP。
如果两个站点都托管在同一台服务器上,那是完全可能的。首先,您必须制作一个 PHP 文件,例如 latest-post.php。并添加下面的代码
<?php
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . "/wp-load.php");
?>
<?php
//The Loop - latest 5 posts from blogs category
$query1 = new WP_Query('showposts=5&cat=blogs&offset=0');
if ($query1->have_posts()) :
while ($query1->have_posts()) : $query1->the_post();
?>
<div class="whatever you want to style">
<h2><!--The Title-->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h2>
<!--thumbnail-->
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('Thumbnail'); ?>
</a>
<!--Excerpt-->
<p> <?php the_excerpt(); ?> </p>
</div>
<?php
//loop ends
endwhile; endif; wp_reset_postdata();
?>
将此文件放在您的索引文件所在的非 wordpress 站点上,并在您想要的位置包含上述 latest.php。
<?php include_once("latest-post.php"); ?>
如果您使用的是 HTML 文件,PHP 将不会执行。您可以将索引文件 .html 重命名为 .php 或添加
AddType application/x-httpd-php .html
到您的 .htaccess。看看 从 HTML 运行 PHP
将数字“showposts=5”更改为您想要的帖子数,并将“cat=blogs”更改为“cat=your category name”