WordPress.org

Ready to get started?Download WordPress

Forums

Solution to display recent posts from blog to website (3 posts)

  1. Dubai Web Design - incirclemedia.com
    Member
    Posted 1 year ago #

    <?php
    	$db_username = '#######';
    	$db_password = '#######';
    	$db_database = '#######';  
    
    	$blog_url = 'http://www.#######.com/#######/blog/'; //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 }
    	  @mysql_close($con);
    	?>
  2. Filip
    Member
    Posted 1 year ago #

    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

  3. bcworkz
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic