从WordPress站点拉内容上显示HTML网站[关闭](Pull content from Wor

2019-10-18 18:16发布

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

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

Answer 1:

如果这两个网站托管在同一服务器上,它只是可能。 首先,你必须做,例如最新的-post.php中的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不会exicute。 您可以重命名索引文件的.html到.PHP或增加

AddType application/x-httpd-php .html

到你的.htaccess。 看看从HTML运行PHP

更改数字“showposts = 5”你想要多少职位,并改变“猫=博客”到“猫=您的类别名称”



Answer 2:

Here is an example what I am using on my site to read the headings from the database:

<?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>";         
  } 
?>

Should be easy to adapt to also read the contents if needed.

If you dont have direct access to the database, the following script accesses the blog items from the RSS feed.

<?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"; 
    }     
}
?>

Both solutions use PHP.



文章来源: Pull content from WordPress site to display on HTML site [closed]