从WordPress网站拉取内容以在HTML网站上显示

时间:2013-10-08 07:10:48

标签: html wordpress

我有一个WordPress网站上有博客,但我也有一个HTML / Dreamweaver网站。

我想在更新WordPress网站时自动在我的index.html页面上显示WordPress博客。

2 个答案:

答案 0 :(得分:1)

以下是我在我的网站上使用的示例,用于从数据库中读取标题:

<?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。

答案 1 :(得分:1)

如果两个站点都托管在同一台服务器上,那么它很简单。首先,你必须制作一个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。看一眼 Run PHP from HTML

将数字“showposts = 5”更改为您想要的帖子数量,并将“cat = blogs”更改为“cat =您的类别名称”