Pull content from WordPress site to display on HTM

2019-08-12 14:41发布

问题:

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.

回答1:

If both sites hosted on same server, it is simply possible. First you have to make a PHP file for example latest-post.php. and add the code below

<?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(); 
?>  

Place this file on your non wordpress site where your index file is and include the above latest.php where you want.

<?php include_once("latest-post.php"); ?>

If you are using HTML file, The PHP will not exicute. You can either rename index file .html to .php or add

AddType application/x-httpd-php .html

to your .htaccess. Take a look at Run PHP from HTML

Change the number "showposts=5" to how many posts you want, and change "cat=blogs" to "cat=your category name"



回答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.