Support » Plugins » Hacks » Solution to display recent posts from blog to website

  • <?php
    	$db_username = '#######';
    	$db_password = '#######';
    	$db_database = '#######';  
    	$blog_url = ''; //base folder for the blog. Make SURE there is a slash at the end  
    	//connect to the database
    	$con = mysql_connect(localhost, $db_username, $db_password);
    	@mysql_select_db($db_database) or die("Unable to select database");
    	$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 2";
    	$query_result = mysql_query($query);  
    	while($row = mysql_fetch_array($query_result))
        <div class="blog_cont"> <a>"><b><?php echo $row['post_title'] ?></b></a><br clear="all" />
          <span><?php if (strlen($row['post_content']) > 40) { echo substr($row['post_content'], 0, 120) . '...'; } ?></span> <a>">Read More ยป</a> </div>
        <?php }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello, thank you for this snippet, works great with recent posts.

    i was wondering do you have any idea how could i make it work if i have a custom query?

    thank you very much in advance

    Moderator bcworkz


    Hi Filip – You need to translate your custom WP_Query into a raw mySQL query string and assign it to $query in the above snippet. I’m not good enough with mySQL to do that for you, but I know you can see the raw query used by WP_Query by hooking the ‘posts_request’ filter. You can’t do this dynamically because WP is not initiated in the website. You can use the information to hardcode into the above snippet though.

    The other issue with not having WP initiated is you cannot use the WP “loop” and the associated template tags to display the results of your query. You’ll need to work out a reasonable display using only the raw post object data and standard PHP functions. Any WP functions will not work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Solution to display recent posts from blog to website’ is closed to new replies.