Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Adding latest posts from blog to website

Featured Replies

Is this possible?

 

I've got a wordpress blog that I've created for a client as well as a standard website and was wondering whether I could pull the top 5 latest posts into the website somehow.

 

Anyone got any ideas?

 

Thanks

 

E

Hey

 

I'm not sure but i just intergrated my client's blog into the website; www.fishercarpentry.co.uk/wordpress

 

Dunno if this helps :(

  • Author

Hi there - nope that's not what I mean as I have that set up already in that the client sitre is at

 

http://www.clientsitehere.com and the blog is at: http://blog.clientsitehere.com

 

 

what I want to do is have a widget or feed from the blog to appear in a small area of the News page and for it to pull in the latest 5 posts. People would be able to scroll through these and click them to then get taken to the blog post in question.

  • Author

don'r really understand that sort of stuff...

If you add this php to a page it will do what you want - I tried it on one of the pages on my site

 

<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./wordpress/wp-blog-header.php');
query_posts('showposts=10');
?>
<!-- mini loop for the excerpts -->
					<?php while (have_posts()): the_post(); ?>
					   <h2><?php the_title(); ?></h2>
						  <?php the_excerpt(); ?>
					   <p><a href="<?php the_permalink(); ?>" class="text1">Read more...</a></p>
					<?php endwhile; ?>
					<!-- end the mini loop -->

  • Author

eh?

 

How does that work?

Where's it pulling the posts from - there's no reference to the blog in there.

 

Can you let me know what I would need to change to personalise it to my site.

Yes.

 

Install WordPress on your server then add this PHP code at the very beginning of your XHTML document, before the doctype.

 

<?php  
//db parameters . You can find this information in your wp-config.php file.
$db_username = '###';  
$db_password = '###';  
$db_database = '###';  

$blog_url = 'http://www.yourdomain.com/blog/'; //base folder for the blog  

//connect to the database  
mysql_connect(localhost, $db_username, $db_password);  
@mysql_select_db($db_database) or die("Unable to select database");  

//get data from database  
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 1";   

$query_result = mysql_query($query);  
$num = mysql_numrows($query_result);  

//close database connection  
mysql_close();  

//assign data to variables  
$blog_date = mysql_result($query_result, 0, "post_date");  
$blog_title = mysql_result($query_result, 0, "post_title");  
$blog_content = mysql_result($query_result, 0, "post_content");  
//$blog_permalink = mysql_result($query_result, 0, "guid"); //use this for 'p=11' format  

$blog_permalink = $blog_url . mysql_result($query_result, 0, "post_name"); //combine blog url, with permalink title. Use this for title format  

//format date  
$blog_date = strtotime($blog_date);  
$blog_date = strftime("%b %e", $blog_date);  

//how many characters should be shown?  
$maxchars = 135;  

//strip out the html tags, such as images, etc...  
$blog_content = strip_tags($blog_content);  

//cut down the size of the post to 135 characters  
$blog_content = substr($blog_content, 0, $maxchars);  
$blog_content = $blog_content . "...";  

// html page starts after ?>  
?>

 

Add the following within the DIV to display the latest post content and title from your WordPress blog.

 

<?php echo $blog_title; ?>   

<?php echo $blog_content; ?>

 

Then just style as required.

 

Hope this helps.

  • Author

WDL - is your option customisable - the box I want to load posts into is tiny and I would prefer that it only pulled in the title & summary or just the title of each post?

  • Author

I'm getting an Unable to select database error and don't know why as the username/password/database info is correct

Basically, just either add <?php echo $blog_title; ?> where you want the title to be displayed and leave out <?php echo $blog_content; ?> or just edit the $maxchars variable from 135 characters to whatever you wish.

You shouldn't be getting any database errors if the info is correct. You must have typing errors or incorrect URL's.

  • Author

I'd missed a letter out of the database name and it's now working...also had to remove the comment line at the end as that was causing issues as the closing php bracket wasn't commented out leaving two n place.

 

Now I've just got to style the thing - I've only got one blog post on the blog at the moment, so only one is getting pulled through - does it pull more than one post or am I restricted to one?

  • Author

Yes it's working now...another question...it just displays the posts...is there a way to make them link to the actual blog?

  • Author

I guess I could just add a link around the blog title bit within the HTML...actually that wouldn't work would it as the posts are going to change.

Glad you sorted that out.

 

Try changing the LIMIT from 1 to whatever limit you want?:

 

$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 1";

  • Author

I added a couple of more posts to the blog and changed the limit to 5 - still only showing the latest 1

Yes you can link to the blog post fairly easy. Just do the following around the title like so:

 

<h2><a href="<?php $blog_permalink; ?>" title="<?php $blog_title; ?>"><?php $blog_title; ?></a></h2>

  • Author

that link thing deosn't work - it just make the title disappear.

  • Author

back to the drawing board for me as I'm not a PHP coder and so I'm off to look for an off the shelf widget instead.

  • Author

i responded to your post NeRo...

 

eh?

How does that work?
Where's it pulling the posts from - there's no reference to the blog in there.

Can you let me know what I would need to change to personalise it to my site.

 

but then WDL made a suggestion and I tried it out to no avail.

 

I'm now looking into Google gadgets

  • Author

I'll have a look through that article - thanks for the link.

 

I'm looking for a widget of some sort that's totally customisable now as I need a solution for the current site & blog I'm working on - both PHP based, but I need to find a non-php way of doing this as well for another site that will have a PHP based blog but the site is ASP based...hence the search for a widget.

i responded to your post NeRo...

 

WDL's post got in-between and I thought your reply was to him ;)

 

re your question, it is simply a case of changing the "require" to point to your wordpress e.g. ..

 

require('./wordpress/wp-blog-header.php');

  • Author

I've just read that article and implemented what it suggested and it works a treat - I've even managed to customise it to look right and also used the following instead of it's suugested <?php the_excerpt(); ?>

 

<?php while (have_posts()): the_post(); ?>
		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
		<?php
		// Display content above the more tag
		$more = 0;
		the_content("More...");
		?>
		<br />
		<?php endwhile; ?>

 

So thanks gents - you've made what has been quite a misearble day of widget hell into something totally customised to suit my needs!

 

Just need to figure out how to do something similar on an ASP based website now.

Yes I saw your reply. My solution didn't seem to work for Eskymo so I referred him to your link.

No offence WDL there is no need for all that custom code.

 

WP has a ton of tags and if you use them, your coding footprint is tiny.

 

<?php query_posts('showposts=5'); ?>

<?php while (have_posts()) : the_post(); ?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

<p><?php the_excerpt(); ?> </p>

<?php endwhile; ?>

 

That will select the 5 latest posts from all categories. If you want limit the amount of posts, you add something like this to the query_posts function

 

<?php query_posts('showposts=5&cat=23'); ?>

<?php query_posts('showposts=5'); ?>

<?php while (have_posts()) : the_post(); ?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

<p><?php the_excerpt(); ?> </p>

<?php endwhile; ?>

 

that's not complicated is it?

  • Author

widgetbox is awful - i tried that first and found it too annoying - it's ok if you don't mind advertising the product.

I'll have a look at feedburner as I use that for my personal blog and see what they offer - thanks for the tip.

<?php query_posts('showposts=5'); ?>

<?php while (have_posts()) : the_post(); ?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

<p><?php the_excerpt(); ?> </p>

<?php endwhile; ?>

 

that's not complicated is it?

 

Use this!!!!! This is what WordPress was designed to do!!! No need to go get feedburner to go to your site, aggregate the content, then re-display it on your own site!!! This is redundant. You have a database of posts, you should use it! LOL really you should be friends with your database and know it's every detail. :)

  • Author

Using feedburner might be useful for me as I can't use the above script on an ASP based website!

Sorry I read this post entirely wrong.

 

I saw WordPress and the blinkers came on. Well I don't know about .asp but isn't it possible for you to make an AJAX call and bring the content in that way?

 

Also, .asp can access a database like PHP so why don't you just query the DB using .asp? You mentioned about complex coding... So yeah I think I'll end the input here. lol

 

Sorry for the distraction :(

  • Author

Well if I can access the database of the blog and pull in the info that way, then I could get my ASP programmer to do the technical stuff there. he'll understand what "query the DB using ASP" means. He doesn't do PHP at all so the blog integration part is going to be interesting

Yeah give him this info...

 

He needs to grab data from the WordPress database, more specifically the wp_posts table to grab the latest 5.

 

He'll probs know more about it than me with that language, just point him to your database and you should be good to go.

 

If it comes back a no go - don't be shy to get your hands dirty with .asp coding. There are a few .asp gurus around here.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.