简体   繁体   中英

wordpress blog connects with php website

I have wordpress blog and php website. I'd like to put the information about new articles on wordpress blog to the php website. I did something. I have titles of articles. But I need featured images and descriptions as well.

Here is code which I put on my php website.

<?php 
      // connection with wordpress
$db = mysql_connect ("localhost", "...", "....");
mysql_select_db ("...", $db);

$result_redaktor = mysql_query("SELECT * FROM wp_posts WHERE post_status = 'publish' ORDER by post_date DESC LIMIT 10", $db);

if (!$result_redaktor)
{

exit (mysql_error()); 
}

if (mysql_num_rows($result_redaktor) > 0)

{
$myrow_redaktor = mysql_fetch_array($result_redaktor);

do
{
printf ("<div class='main'>
    <a href='http://redaktor.11klassniki.ru/%s'>%s</a></div>"
    , $myrow_redaktor["guid"], $myrow_redaktor["post_title"]);
}

while ($myrow_redaktor = mysql_fetch_array($result_redaktor));
}
else 
{
echo "<p>no data.</p>";
exit ();
}
include ("blocks/bd.php");
?>

I don't know how to get featured image and text (lead, which goes before tag more).

PS My site and my blog are in the same hosting.

This code allows to see titles. It works. But I need to add pictures to titles.

How can I customize next code

  $thumbnails = get_posts('numberposts=10');
  foreach ($thumbnails as $thumbnail) {
    if ( has_post_thumbnail($thumbnail->ID)) {
      echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
      echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
      echo '</a>';
    }
  }

  echo get_the_post_thumbnail( $post_id, $size, $attr ); 

You should use wordpress RSS, read this article for more info: http://www.wpbeginner.com/beginners-guide/what-is-rss-how-to-use-rss-in-wordpress/

Or, if your site and your blog are in the same hosting, you should include wp-load.php file in your php site and use all the function wordpress to show posts.

One example from http://davidwalsh.name/wordpress-recent-posts :

// Include the wp-load'er
include('wp-load.php');

// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 10
));

// Do something with them
echo '<ul>';
foreach($recent_posts as $post) {
    echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';

Enjoy your code!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM