Display Wordpress content outside of your blog
Wednesday, May 7th, 2008
Wordpress is a very powerful and flexible content management system (CMS) for blogs, and is among the first choices for anyone looking for a way to include dynamic content on their web sites. When using Wordpress to power a news or blog section of an existing web site, authors will often want to display a list of headlines or the latest post on the front page of the site, outside of the Wordpress-powered section. Using a bit of PHP and the Wordpress API, this is easy to do.
For the purpose of these instructions, I will assume that your web site URL is www.example.com and that wordpress is installed in a subdirectory at www.example.com/blog. I am also assuming a basic level of familiarity with HTML and editing files for the web.
The key to gaining access to the power of Wordpress from an outside page lies in the wp-blog-header.php file. This file loads the Wordpress application and makes its API, and therefore your content, available for use. Once this file is included in a page on your site, you will be able to use any Wordpress function just as if you were working in a Wordpress template.
Let’s add the latest post to the front page of our site. We’ve already got a blog at www.example.com/blog/ but it would be nice to show the latest entry alongside the rest of the information on the front page. If your front page is already a PHP file, you’re ready to go; otherwise you’ll probably need to rename your front page file extension from .html to .php. (Be sure to test to ensure that doesn’t break anything!)
The first thing we’ll do is pull in that all-powerful Wordpress file, which we can do by adding this section to the very top of the page you want your post to appear on:
<?php // Include Wordpress define('WP_USE_THEMES', false); require('./blog/wp-blog-header.php'); query_posts('showposts=1'); ?>
The first line of this block is simply a comment describing what we’re doing. The second line tells Wordpress not to display your templates, and the third line grabs that file I told you about. The final line performs a database query to retrieve the content we want to display on this page. In this example, we want the single most recent post, but the query_posts() documentation shows how to retrieve a variety of data.
Now we’ll move down in our page and find a good spot for this post. Once you’ve figure out where to put it, we’ll add a Wordpress loop which will display the single post retrieved above:
<?php while (have_posts()): the_post(); ?> <?php endwhile; ?>
Once the loop is in place, we just have to decide what parts of the post to display, and write some HTML for them. Let’s say we want the post title and just an excerpt, followed by a link to the full article. We’d do something like this:
<?php while (have_posts()): the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> <p><a href="<?php the_permalink(); ?>">Read more...</a></p> <?php endwhile; ?>
If you’ve been writing Wordpress themes, you’ll immediately recognize the template tags used here. We’re using all the same functions for the title, excerpt, permalink, etc, that we do in a Wordpress template. Including wp-blog-header.php makes this all possible.
Bonus
You can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the Wordpress directory on the original site. Simply modify the require() in the first block on this page to use the full path to your Wordpress installation:
<?php // Include Wordpress define('WP_USE_THEMES', false); require('/var/www/example.com/blog/wp-blog-header.php'); query_posts('showposts=1'); ?>
Note: PHP restrictions such as open_basedir may prevent this last example from working.
Update
Upon further research, I’ve discovered that this technique cannot be used to pull content from more than one Wordpress site. If you need content from more than one, try using the RSS feed from the second (third, etc). You won’t get full API access the way you will using this method for the first, but at least it will get you headlines and content. See my article on displaying RSS feeds with SimplePie if you need help with this.


Incoming Links
Comments
Hi, great post!
How would you call a specific page as opposed to the latest post.
Thanks!
Easy! Just change the arguments to
query_posts()to include the page ID or page slug:Thanks!
Is there anyway to display the comments as well?
Sure. Once you’ve set your query parameters, this is just a regular Wordpress loop, the same as you’d find on an index or single post page. You’re free to include your comments template as needed.
You’re a beautiful man Ken!
And the least I can do for you is spell your name right Kenn! ;)
Can’t possibly thank you enough for this post. Been looking for this for awhile now. Definitely will give the deserved trackback love.
For anyone hoping to do more than just display the most recent post, you can see a full list of “template tags” below. (Template Tags are functions that you have full access to once you include the wp-blog-header.php file above):
Wordpress Template Tags
Thanks again for the post,
Tison
Hey – thanks for posting this! I knew it would be possible to do this with a bit of simple php – but could I find accurate, up-to-date and concise directions anywhere on the web? Not until I found this page! :)
Thanks very much – it worked a treat for me for one of my friend’s clients… I am sure I will be using this technique many times. Thanks for taking the time to publish it.
I was even able, with my limited php/WP knowledge, to just grab posts from a certain category (next auction) that displays on the last post in the ‘next auction’ page… plus a blog/latest news page that displays the last 4 posts to the blog.
Next mission is to find a good ‘recipe’ for integrating the style for the blog from the main site …
Hi,
I have my WP site all set up.
I have an include file which I feature on all my pages(index.php,archive.php,page.php etc).
How do I display a post on one of my pages?
I tried:
but no success :(
Please help,
Many thanks!
It looks like your code was eaten by the comment form, but for general Wordpress support problems, try the support forums or codex.
Holy crap! I almost gave up looking for this info, until I ran across your article – thanks so much!
Your site looks so nice and clean and the content on this page was so clear and easy to understand. Wordpress itself doesn’t even have this info on it.
Thank you very much for posting it.
Is there any way to control the length of the excerpt?
@Adam: This blog post should help you out. http://incoherentbabble.com/2007/06/08/changing-the-length-of-the_excerpt-in-wordpress/
Adam: Yep, the link isaacw posted is pretty much the same method I’ve used when I need to do this. Thanks isaacw!
Hi!!! Thanks so much for this post Kenn!!!
I have a question that I hope you can answer.
How can I display only like the first two lines of the post instead of a big portion of it? I tryed isaacw link but didn’t work…
Thanks so much,
Paolo
The custom excerpt post linked above does work, but the code shown in the article is broken in that it uses curly “fancy” quotes instead of regular ones, so it can’t be copied+pasted as-is. Fix the quotes and it works fine.
Excellent Post! I have been looking for a way to easily pull the same blog info to multiple sites in various ways.
Thank You!
PS
About You:
Kenn Wilson is a Linux system administrator and web technologist in Oakland, California who spends an inordinate amount of his free time researching and keeping up on the latest web trends and technologies.
FYI - Kenn, your time is well spent. I found this post 3rd out of 1M+ searches on the Big G on my first request. Much Success!
Hi,
This is INCREDIBLY helpful, thank you. I’m wondering if I can use this same technique to sort of “wrap” the header/footer files around a page outside of Wordpress. I am rebuilding my site in Wordpress but have an outside application that is set up with a server side include to pull my old html header and footer files. This was down to avoid having to iframe this outside app. I want it to be to mimic this behavior without have to completely recreate my wordpress headers and footers in html. Does that make sense?
Thanks for any insight you have
Jake: Try calling the Wordpress
get_header()andget_footer()functions after including wp-blog-header.php, as shown above. This will display your header and footer templates, just as they do in your WP theme.I’m doing exactly this on my resume page on this site, which is an otherwise static file, not something produced by Wordpress.
I love you so much for this.
how do i get the last 3 posts?
You can control the number of posts retrieved with the
showpostsargument toquery_posts():Is there anyway to use two of these on the same page outside of wordpress?
When I set them accordingly… one to the Zine (runs on wp) and one to my Blog (runs on wp)… it only reconizes the first batch of code… pulling just the blog or just zine…
Any way to pull both on the same page?
thanks!!
Adam: Are you talking about pulling content from two different Wordpress installations? I’ve not done this myself, but it should be possible. For each blog you want to display content from, you should have new
require()andquery_posts()statements.If you just want to run two different queries against a single WP site, you can just add a second call to
query_posts()and a new loop.Yea… its from two different wp installations.
One named /blog/ and one named /zine/… each in the corresponding directories.
[partially-stripped code removed for readability. -- Kenn]
Any help?
Thanks!! :)
Adam: Be sure your
require()statement is correct for each blog you want to include content from. If your WP blogs are installed in /blog and /zine, yourrequire()lines will look like this, respectively:Thank you for the trick. It took 30 seconds to me to make it work :) Great article!
Hi Kenn,
I finally got around to playing with this and it has almost done the trick. In IE it looks OK but firefox has is a little screwy with the pages I’m “wrapping around”. Do you have any advice before I bang my head right through a wall? You’ll see what I mean at http://www.mlsfinder.com/ma_mlspin/myrealtorjake/.
Thanks for sharing all of your knowledge with us!
Jake: It looks like your problem is HTML/CSS related. Get this fixed up and it should be fine. The Wordpress header/footer is displaying, so your WP code is ok.
Hi Kenn,
Thanks for the info.
I want to make 3 blogs, lets calll them A (main blog) and B and C. All different WP installs and different domains. Blogs will be on different servers. The database will not.
On blog A I want to show posts from blog B and Blog C.
Can your require an post from blog B and make it show in blog A although the blogs are not on the same server?
I have tried:
require(’http://www.blogA.com/wp-blog-header.php’);
But this gives me an error.
Also tried through RSS feeds but this limits the way the post is shown.
Thanks for your reply.
Edwin: When you request a file via HTTP, you’re getting the parsed HTML back. In the case of wp-blog-header.php, this is simply nothing, a blank page.
What you really need is the database for Blog A, not the site itself. I haven’t tested this, but you might try installing a second dummy copy of WP on the same server as Blog B but with the wp-config.php file copied from Blog A. Including wp-blog-header.php from this dummy installation will allow you to access Site A’s data via the Wordpress API, even if the site itself is elsewhere.
This is an interesting idea. Let me know how it works out if you try it.
About pulling content from two different Wordpress installations on the same page: it didn’t worked for me; the page shows two times the article from the first blog, the same that is happening to Adam.
I’ll let you know If I work it out.
Chiara: I looked into this while talking to Adam via e-mail. According to the WP codex,
query_posts()may behave unexpectedly if used twice on the same page.I’m using it twice in one place myself and it’s working fine, but if your isn’t you might have to replace your query_posts() calls with new WP_Query objects. The principal is the same. I’ve used this second method as well and it works just as you would expect.
Hi all.
I tried this method on WordPress 2.6.2.
But, don’t working. PHP printing fatal error:
“Fatal error: Call to a member function set_prefix() on a non-object in /home/farkhod/sites/site04/root/wp/wp-settings.php on line 216″.
Can you help me?
Thanks.
Superb thanks very much for a great guide!
Athena Design
Hi Kenn,
Thanks for the info.
I have copied a dummy of WP into a folder called “requiretest”.
When I put
require(’./requiretest/wp-blog-header.php’); into a separate php file in a folder above this requiretest folder it works fine.
But when I make a pagetemplate in which I add this line it gives an error saying that the file cannot be found.
I also tried several variaties of the path, like .././requiretest/wp-blog-header.php and ../.././requiretest/wp-blog-header.php etc…
It will be my lack of knowledge of Wordpress that I can not seem to find the correct path.
My original WP install is in the folder “nieuws” and the dummy is in the folder /nieuws/requiretest/.
What path should be used in the require command ?
Thank you for your effort.
Edwin
Edwin: I’m not sure what you mean by “pagetemplate”, but I suspect you may be putting this code into the theme file for another WP site. This should only be used on a static PHP page outside of any WP theme.
If this is not the case, there may simply be a problem with your directory structure or PHP configuration. It’s impossible to say without actually taking a look at it.
I’m available for Wordpress consulting on an hourly basis if you need help troubleshooting. Get in touch for my rates if you can’t get it working.
Hi Kenn,
You are right, I mean a template within an WP theme.
Perhaps I can work with an static PHP page. I will rethink the way I want to set this up.
Could very well be that I will consult you in the future.
Thank you for your help.
Edwin
Hey Kenn!
When you said to Adam:
“If you just want to run two different queries against a single WP site, you can just add a second call to query_posts() and a new loop.”
How is this possible? I am trying to call from my main blog in the sidebar, but I want to be able to define each page to display a wordpress page’s content.
Is this possible as one query I want the last 7 posts, the other I want a page…
Thanks,
Seb.
Seb: This isn’t a problem. Scroll up for my comment to Chiara, which also mentions Adam’s question. If the two calls to
query_posts()don’t work, try replacing them with new WP_Query objects. This will allow you to pull different content in two different places.Pardon my complete ignorance, but if I’m trying to call the latest 7 blog posts, but also a page, from the same install… Is this possible?
I tried simple doing
But this simply replaces the recent blog titles that are in my sidebar with the page title.
What I’m looking to do, is have seven latest blog posts in my sidebar – (currently formatted like this):
<b><a href="" rel="bookmark" title="Permanent Link to " rel="nofollow"></a></b>Posted on:
And then put a page from the blog as the main content… Eeek.
Thanks again for your help Kenn.
Seb.
Is there a way to show on the blog part of a web page or a frame from a website outside the blog?
There’s a website giving the menu-of-the-day for my kids. I’d like to show this menu of the day in some part of my blog. So I need something that retrieves this info from the website and shows it “as it is on that site”, not just as a link.
Hope I made myself clear enough. I’m'very (VERY) poor at html, php and web design….
Thanks
Seb: Your code was eaten, but you should be able to do what you want. I’m doing something similar in the footer of this site. Hard to guess at the problem without playing with the code though (I’m available for WP consulting if you really need help).
Francesco: Unless the other site has an RSS feed or you can write a screen-scraper, you probably want an
<iframe />. Simple, but will probably get the job done. You can find help with this in an HTML forum, as this really isn’t Wordpress related.Great information you’ve been given out. I’m running into an issue where I want to register new users into my WP from another page on my site, how do I go about doing that. Meaning I am integrating the WP Blog into a site and I want to be able to have people who register for my site automatically register for the WP Blog at the same time. That’s why I need to insert a new user into the WP at that time. I tried the following:
But it seems to conflict with my database for my site is throwing errors when I make any db calls after wp-blog-header.php has been included. Any ideas what I could do to get this to work?
Dre: Your best bet would be to post this over on the Wordpress forums. Don’t forget to post the actual text of the errors you’re getting.
That works a treat.. I have a few questions:
1. What other options are available if I want to show Author, date and time added, Comments, Image etc etc..
2. What do i cange if I would like to see a catgeory rather than individual posts?
Thanks
Mr Wilson, you are gentleman and a scholar! This is just what I was looking for. Works beautifully!
~David
Hi there!
this article is good but I have a question:
if I would display the IMAGE
uploaded with the article
outside the loop but in the same page ..??
it’s possible to do..?
Awesome!
Thanks very much for the post, this saves me a lot of time.
Awesome post Kenn – This is going to be great to allow me to retro fit some WordPress info from the blog on my non-WordPress homepage. Can’t thank you enough.
S.
What if you need to do this, but cannot change the home page to a .php file? I am using a CMS called OpenACS.
domdeez: I’m sure it’s possible, but OpenACS is not written in PHP so you won’t be able to easily use the Wordpress API we’re using here. Your best bet would be to find a person or support group for people running this particular application and ask for their advice.
What a fantastic piece of code. THANK YOU!
We added WP this week, and I’m just learning. Your code is just what we need to make it more user friendly on our opening page.
I do have one issue. One post is putting high ascii A after some lines, and I can’t figure out why. It displays fine in WP, but not through this code. I checked the HTML verson, in the editor, and it has no coding or special characters where these high ascii characters are displaying.
I’ve pulled out most of my hair on this one:) It does the same with IE7 and FireFox.
Any suggestions? the test code is at neppfa.com
Thanks in advance!
Mike
Great stuff Kenn.
I suspect if this sort of integration was second nature at the outset, and properly promoted, Wordpress could easily be attracting an even greater following. A premium blogging tool with a simple, bolt-on CMS—now that’s something to build on. Thanks for helping this great software turn the corner when it comes to sites that utilize a blog, but are not built around them.
Thanks. Helped me a great deal. Keep up the good work.
Thanks for this! Elegant method that works great…
Hello mate, just a quick one to say thanks very much for the post … couldnt find this info in the Codex, and been searching for plugins etc, but this made it a lot easier.
I can go to bed now!
Thanks Ken, this works well and is clearly explained. Now for my question… :)
I’m setting up my index page to display 20 recent blog posts, and using the code below to view older posts. How do I set up an “Older Entries” page to match my index page? Currently clicking Older Entries gives me an unstyled page with all entries.
Thanks!
Mike: This sounds like a general WP theme problem. If the page appears unstyled, there’s likely something wrong with your paths. Look at the source of the page and see where it’s looking for your stylesheet.
Thanks for the reply. I’m actually trying to display blog posts solely on my non-WP index page, and not link back to a WP theme page. Do you think it’s possible to display older entries this way? I’m not going to include comments, archive, or anything more elaborate.
Mike: You might be able to hack something together with
query_posts(), but it might be easier to rethink how you’re building this site. WP’s standard archive template can do exactly this for you with basically no extra work. If nothing else, you might be try copying the code from a working WP archive template into your desired page to see if you can get it to work.I think you’re right…I started going in the other direction and just tweaking a WP theme to match the existing site. Your tutorial at least helped me understand how far the loop can be pushed though.
Thank you for a very understandable and helpful guide!
I have one question, though I fear it is a bit complicated. I am setting up seven different blogs on the same domain, with a shared frontpage. I want both static and dynamic content on the frontpage, and is hoping to create a “10 last updates” aggregated from all seven blogs for the front page (links to the title of total 10 posts with the name of the blog in brackets after each title). Is this possible?
Or is it a better strategy to merge the RSS-feeds and show the last 10 updates? (I am not sure how to do this neither).
Magnus: Definitely just use RSS for this, anything else is madness. Take a look at SimplePie for an easy way to fetch and merge your feeds.
Thank you so much for this! I’ve been trying to get this to work using several different types of code snippets, but this finally did it. So simple, too, once you explained it so clearly.
Hi Kenn,
Thanks for posting this.
I’ve been trying to do the same with my kurbick theme. My problem is that I also want to add to this static page that “looks like” my blog my sidebar to the right side of the screen, like in my blog.
The thing is that if I put the before the content of my page, the content (a form) gets pushed down below where my sidebar end, and if I put the after the content, then it is the sidebar that gets pushed down under where my form ends.
to make it clear:
scenario 1-
---------------------------- | B A N N E R | ---------------------------- |---| | | |---| content contentscenario 2 -
---------------------------- | B A N N E R | ---------------------------- content content |---| | | |---|Sorry for the space :)
Do you have a suggestion for me?
thanks!
Sorry for that unsuccessful attempt of drawing a side bar. That thing that looks a bit like a car is the side bar and it was supposed to be drawn on the right side of the “screen” drawing.
Hope it is clear.
@Boaz: No worries, I fixed it so it displays properly. But your problem is probably in your CSS, not the WP code. Check how your sidebar is floated and be sure you’re putting the page content in the right place. You may have it within the wrong HTML element.
Thanks Kenn…
I sure have a lot to learn about CSS and HTML :)
I will try that. I wasn’t aware of the float property to tell you the truth !!
Your site was a godsend! I am not a blogger, but have an Herb website that I decided to add a blog to in order to get participation, etc. Your post here was finally found and worked..see page I put it on in the right main column. What I want to know is how do I include the whole post, not just an excerpt and I want to be able to have people comment, etc. ( ie include everything from the post on WP, date, author, etc.) on this page or else I might as well just write my post on this page.
I hope that was clear. I truly thank you for this, I have been looking a long time!
@James: For the full post instead of an excerpt, use
the_content()instead ofthe_excerpt(). There’s more information on the various functions you can call here on the WP template tags page.That said, I think you’re going about this backwards. Displaying an excerpt or full post on a non-WP page is no problem but if you want other functionality like commenting, you’re better off theming WP to match the rest of the site and using it normally.
I’m available for WP consulting and customization work if you get stuck.
Thank You Ken for your response and advice. I may be calling on you. Have a great New Year and thank you again.
as Paul Hudson asked:
What other options are available if I want to show Author, date and time added, Comments, Image etc etc..
I’m also interested in knowing more about this.
@asundrus: Take a look over the WP template tags documentation. You have access to the full Wordpress API here, so you can use any template function you would use in a regular WP template file.
Okay, this is what ‘I have in the page where i’d like stuff to be posted:
And where would I add say:
Date posted:
cause I tried, but it only shows once including date, the rest of the posts just shows “Date posted” and nothing else.
Damn, sorry for double post but it didnt show the php code.
But I used your example, and tried adding stuff from the link you provided.
@asundrus: Take a look over the documentation for the loop and make sure you’ve got everything set up correctly.
I’m available for WP consulting and customization work if you have trouble getting it working.
Thanks so much for this, it really helps. Now, is their a way to link to the next (or previous) entry in a category using this method ?
i just wanted to say thank you SO much for this article! it was so helpful to me.
Great article. Just what my client is looking for. I’m going to play around with the code to see if I can add the blog’s feed to the client’s front page.
And where’s your tip jar, Kenn? I’d hit it. :)
OMG THIS IS AMAZING! AMAZING! I FINALLY UNDERSTAND!
@Joni: No tips necessary, (but I do have an Amazon wishlist…).
But no worries, I’m happy to help.
Really useful post – thanks so much :)
Hi Kenn,
In regards to pulling info from multiple blogs on one page…for some reason I have not been able to make this work. I’ve tried various combinations of code, but it seems that whatever blog is called first, will set the stage for the second loop. The code below is a simple example of what I’m trying:
[scrambled code removed for readability. -- Kenn]
Any thoughts would be great :)
Kenn,
I just wanted to say thanks for this info! It worked like a charm and only took a minute or so to incorporate into my site!
I did figure out to have href, h1 and other html tags function, I needed to use ‘the_content’ rather than ‘the excerpt’ (which works fine for what I’m doing). I’m not sure if this is because I’m pulling a page rather than a post, but just thought I’d share for those of you having issues with links and styling not coming through.
Thanks again!
@Corwyn: Your code didn’t come through so it’s hard to guess at the problem, but it looks like you’re creating WP_Query objects. Make sure each subsequent WP_Query object is created properly; try using a different variable for each to avoid any confusion about what’s what.
Thanks Kenn for the feedback. Still stuck though. Here’s a link to my code that I’ve been trying…maybe you can see the error.
http://www.mysteinbach.ca/test-code-multi-loop.php
Kenn, Hello. Your advice has helped me display posts on my static site and it works great. I have numerous categories that my partner posts on her blog and asks if I can transfer them instead of on one page as I do now but to the appropriate categories on our html site. Using the Green Homes category as an example, it has a category id or page id of ?cat=30. How do I use this to call only those posts from this category on my wordpress site and display them on my html site page Green Homes?
Thank you so much…
What I’m trying to do is this..
I have my main blog, and I have a food blog. On the food blog, I’ve created (using your technique) a hard file called random.php that lists 3 random posts from one category. It works great. Example:
http://www.twenty-eight.org/food/random.php
Now what I want to do is include that file in the sidebar of my main blog. The problem is, when I included it, it linked to the posts on the MAIN blog, instead of the posts on the FOOD blog. So it says “category 7″ and went and found posts in the main blog’s category 7.
How do I make it NOT pull in data from that blog, but pull in data from the food blog, exactly as it says on the page when you go to http://www.twenty-eight.org/food/random.php
THANK YOU!!
@Corwyn, @debi: I take back what I said earlier about using this technique to pull posts from multiple WP blogs. Upon further research, this doesn’t seem possible. Try using the RSS feeds instead. You don’t get as much control but it will get you headlines and content.
@James: You can restrict your query results to a specific category, more than one category, or any number of other things. Take a look at the
query_posts()documentation for details.Thanks Kenn…I’ll work with that.
Thanks Kenn. I did just that and now have posts from specific categories, showing up on their specific pages. Thank you.
Kenn, I ended up using the RSS feed. I wanted to do random posts, not the last posts but.. oh well. Can’t have it all!
How can I display just the post title and make the title clickable instead of adding another line “Read more…”
Thanks for any help…
Is there a way to link excepts to full articles using this method within the “host” website.
For example, I use this method to pull in excepts from WP that have “more” at the footer. If I click this I am redirected to the full article on the WP blog. Is there a way to have the content load within the same webpage or site.
BTW. Great idea. Thanks for sharing.
@David: There are undoubtedly more complicated ways to do this but a simple way would be to call both
the_excerpt()andthe_content(), placing the latter within a hidden div. Then add a little JavaScript to hide the excerpt and display the content div when the reader clicks the “read more…” link.A more complicated method would be to use Ajax and/or the the Wordpress XMLRPC API. Personally, I’d go simple.
Thank You!!! Now I can replicate the news on other site
Kenn…
thanks for this post. Keep it up!!!!!!!!!!!!
Thanks for this, Ken. You’re a star.
You sir, have saved me so much time!!! Thank you!
I have looked for three days for this information and was very excited when I found it. As I trolled through the comments looking for MY answers I see the exact scenario posted by Edwin.
It appears that your info will not help. I will explain my situation.
I have 4 different installs of Wordpress on my hosting account. Three of these sites are on specific categories, and the fourth or Main site is to be a catch-all from the other three.
So… I am really trying to find a way to do all my posting (separated under the 3 categories), on the Main blog and have the posts copied somehow to those other 3 blogs with the routing based on category.
Again I have MAIN blog with categories of: Photogrophy, Music and Worship. These are the three other blogs (with separate installs).
Can I do what I need with this info you have here?
One thing I didn’t try was copying the config and wp-page-header.php over to use the same database. That is something I am going to try.
Lastly, and this probably sounds dumb, where does the page header call get placed? is it in the “editor” within WP for index.php or do you textedit the raw page you want in on?
Thanks!
@fretbuzz
Do you NEED four installs of WP? You could always just set up your main categories as pages and have the home page as a catch-all. By doing this, you can define the CSS for each page and have them appear to be inherently different while all being maintained from the same blog. This would eliminate the need for routing from the main blog to the sub-blogs. To do this you would just need to set up some php definitions inside your page.php file to make each part of the blog do what you want. Again this is just a thought as I’m not sure how to do it the other way.
To change the call to the page head, just open up index.php in a text editor and it’ll be at the bottom. Wordpress links to a detailed explanation on this from the settings page.
- Heath
These “other” installs are the only way I know to have the sites appear to be different Identities.
Here is a link to vid that showed me. This was for a teacher site.
http://educhalk.org/blog/?p=34
He is the one that led me here.
I think the easier way what you can do, is getting the RSS Feed and parse the XML.
Nice article.
@Fabian: Sure, if you just want headlines and text, RSS is a great way to do it, and I’ve done that before myself. But if you need a little more flexibility and want access to the full Wordpress API, this is the way to go. It all depends on your needs.
@ken: You are so right here. RSS is NOT the way to go. I want full, polished threads.
Thank you! This works great! Question: is there a way to limit the amount of excerpt text from a post? For example, what if I want to display the first 25 words from the excerpt?
Thanks again for sharing this!
@Rudy: Yes and no.
the_excerpt()doesn’t take any options, but you could always write your own replacement that takes the full post as an argument and then shortens it however you like. This technique is not uncommon among people who need a little more control over their excerpts.Thanks for this Kenn,
This was on my wish list for doing later…
Worked first time – beautifully! I was looking for something else but decided this should be done first…
Elliott
Hi,
I’ve used your code on my site, and credited you in the blog.. I have a tiny but nonetheless annoying little problem. On the sitemap I show a comma delimited list of last six posts by title. It works except it is adding a space on the end of the title…. First Post , Fiddling .
I’ve ruled out stray spaces in the code (which is outlined below)
$title = the_title('', '', FALSE);echo trim($title);
Any thoughts?
Fixed it!
Weirdly enough having some of the code on different lines (to aid reading) added an extra space… When I changed the code to all one line the space disappeared.
I am now pulling posts fine and having it populate the page, but is there a way to pull sidebar widgets and place them in the sidebar of a non-WP website some how with this?
Hi, great stuff. SO clear to understand
HOWEVER, I want to list 5 headlines, in a bulleted list, and no excerpts. I got most of the way there, but cannot get the bullets working right. (Can’t get a full list, only 1 item.)
Hi Kenn,
Thanks so much – this is great and so clear! My question might be off-topic – if so, apologies.
I’m trying to use the header.php file from a Wordpress template outside of my Wordpress install – specifically I’m trying to use it as a header in another program, which unfortunately has a couple of function names in common with Wordpress. Therefore when I use your trick above, I get errors (due to function duplication).
I know I could duplicate the header content outside of WP to keep the style – and boy am I close to doing that! – but ideally of course I’d maintain my header file in just one place, and that place would be within WP so I could use all the yummy WP functionality to maintain it. I’ve played around with auto-caching the header.php file but I’m not a PHP guru and haven’t been able to figure that out yet.
Any ideas would be most welcome — thanks!!
Michelle
@fretbuzz: No idea, I haven’t tried it, but you might just try calling
dynamic_sidebar()and seeing what happens.@Bob: Make sure you’re using
query_posts('showposts=5')instead of only 1, as I’m using in this example. Then just omit the_excerpt() and add your HTML list markup.@Michelle: It sounds like it might be easier to just copy the header rather than trying to force the two apps to play nicely together. Combining them this way may not even be possible. But good luck!
Kenn,
First off, I would buy you a beer for supplying me such great info. I am having a great day today because of you!
I had great success using your suggestions above, but I wonder if you could help me with a couple of questions.
1. I want to include posts from two blogs. The tip of the day would be one, the one beneath would pull from a different post.
Also, as you can see at the test page I have setup at http://bonaventureshoes.com/tips.php, the blog text goes beyond the div. Can I contain it in there without overflowing?
Thank you so much for your help!
@Andrew: This technique can’t be used with more than one WP site at a time. I’ve updated the post to this effect. Sorry about that. You’re not displaying anything but headlines and content so parsing the RSS feed should work fine for this use.
The display problems you’re seeing is a CSS issue. Try removing the height property from the containing DIV.
Thanks Kenn,
So I need to install another blog in order to include two different posts?
Thanks!
Nevermind, sorry, I read your update. Thanks again!
One more time bothering you…
Do you have an example of parsing the rss feeds and how to include that in my page?
By the way, the div did have a height on it and was causing the overflow.
Thanks
This works like a charm ! Thanks very much for putting the article together and being well referenced in google.
honestly when I pressed the “Search” button I had only very limited expectations. Now I’m happy !
How do the instructions vary if it is blog.example.com instead of example.com/blog? Or, is that not an easy change?
Thanks.
Sorry. I was just working on another client’s site with a Wordpress blog, but the person who wants to do this has Quick Blogcast so I’m stuck, I guess.
Thanks.
@Andrew: Take a look at SimplePie for your RSS parsing needs.
Thank you so much! I added the contents of my blog to my main page and it works great. I did not even had to change a thing! One question – How can I add spaces between the 5 post I set up?
hey Kenn-
This is great stuff. I’ve had some success with the things you’ve listed here, but I’m getting some strange symbols in my posts.
Example:
“…among commentaries I’ve read seems…”
It does not appear this way on the blog, but does in the excerpt. Any thoughts?
Here’s the page I’ve been working on:
http://www.joshuajmasters.com/default.php
Thanks
Josh
Great work – code worked first time! I use this code to get multiple post id’s.
Works, BUT.. they display in mod time order (default).
Any ideas to get them in the order of the post_in array would be great.
Hi Kenn,
Thank you for directing me to SimplePie, that worked great. Do you have any suggestions for me to split the feeds at http://bonaventureshoes.com/tips.php
I only want one feed in each box and I cannot figure out how to separate them. Any ideas? Thanks much.
@Andrew
A pure php alternative to maybe look into is – http://uk3.php.net/simplexml_load_string (php5 only!)
@Andrew: It looks like you’re passing an array of URLs to SimplePie; is that correct? That will give you combined feeds. To display them individually, just instantiate a new SimplePie object for each feed and place them wherever you want them.
Kenn,
I guess I need a hand to hold in order to make that happen. I am new to all of this…
Could you show me exactly how to do this?
Thanks.
Kenn,
Great information. Looks like it’s working great except services like Google, Crazyegg confirmed at – http://www.seoconsultants.com/tools/headers.asp , are returning my site as 404 error. Except for the index page. It’s reachable by users, seems to just be a problem with bot type requests.
Any ideas? I can’t have Google not indexing it.
It’s hosted with GoDaddy, they say it’s not them (I wonder though)
Any help is appreciated as I haven’t been able to figure this one out. Here is the url http://www.silvertreeconstruction.com/
WP is installed in /articles Every page except index and /articles return to Google as 404.
@Andrew: I just published an article on getting started with SimplePie that you may find useful.
@Leonard: You’re right, I can confirm this myself using curl:
The page content does load fine however. It must be a problem either with your Wordpress installation or your Apache configuration, but I can’t really speculate much without taking a closer look at what you’ve got set up; drop me a line if you need some help sorting this out.
Kenn,
Great peice of code. Installed and working in a heartbeat. Thanks for sharing.
Steve
Hi Ken-
Thanks for the info. It seems to be a very straight-forward process and everyone has been able to implement it easily. Mysteriously though, it’s not working for me. I would be very grateful if you could offer insight into my problem. I’m using WP 2.7
My code:
Ouput when running php file:
Fatal error: Call to undefined function query_posts()
I know my path to the wp-blog-header file is fine because I get a different error when I change the url, so it seems to be including it fine.. yet I cannot access any of the WP functions!??! Any thoughts?
Thanks!
OK, so now it works when I use a relative path instead of an absolute url, so that’s great. I see now that even when you are accessing the file from an internal server path and not a url so I guess that was my mistake. Sorry to bother you…but maybe my lesson will be instructive to someone else.
@Alex That’s right, when you request the file via HTTP, the PHP is parsed by the sending server and you get back nothing. In order to run the code as part of the site you’re including it on, you need to include it via the filesystem. Glad you got it sorted out.
Very very helpful, thank you for this post!
Wow. This page instruction is so clear & concise. My only (2 day long) difficulty was fixed by changing ‘the_excerpt()’ to ‘the_content()’.
Now my formatting/HTML/images can be seen!
Kenn Kenn the BEST of Men.
Hi Kenn,
Firstly thank you so much for this code. It was exactly what I’ve been looking for and works like an absolute charm.
Just a quick question as I’m not too clued up with writing my own code and leave Dreamweaver to do all the heavy stuff! Is it possible to pull the “post author and date posted” information into the post excerpts?
@Kevin Wallace: You can get the author, date, or anything else by using the standard Wordpress template tags.
Kenn,
Great solution that I’ve been wondering about for a long time. It seems to be working perfectly… except I am trying to pull a page’s content into an existing PHP app and I’m getting an error. You can see the active page here. The content “Store Front” and then the subsequent paragraph are a page in WordPress. Everything after the inclusion of WordPress elements ceases to work properly, it seems because of a conflict on commands.
I guess the reader’s digest version: is there a way to close the WordPress inclusion so everything after will work again? I’ve already tried to place all the WordPress files in their own .php and then include that file, but to no avail.
Thanks!
@Sean: You could put it in an iframe but that kind of sucks. I think I’d dig through your Apache logs and see if I could sort out the code conflict problem. It may be something as simple as a variable name collision.
i am displaying the latest post of my blog on example.php
i have included comment.php and it displays the comment form properly. but it does not show the comments for the post.
how can i provide previous and next link? and can i have the same php file called when someone click on the links and display that particular post(ie either previous or next post)?
how can i allow people to comment and then store it on the main blog?
thanks
Fantastic help! Cheers.
One question: If I set showposts=3 is there a way to display the three different posts in different styles?
My example is I want to create a newspaper style backsheet with the latest post being the main headline and the second and third posts being sub headings further down.
Many thanks,
@Neil: Sure, just add a counter variable in there and increment it every time you go through the loop, and then set different HTML/styles as appropriate.
@Kenn: Thanks! I’ll give that a try.
Keep up the great work!
Great information. Thank you
but what if i want that my permalinks don’t show the name of the directory “blog” so i have links like http://example.com/permalink only ?
thank you
@Gagner: This will do that. The directory Wordpress is installed in will not show in your permalinks. (WP on this site is installed in a directory called
/cmsbut you’ll never see that in my URLs.)Thank you very much i was lookinf for that :)
Thanks a ton for posting such an excellent post.
I spent alot of time searching for it and was getting little frustrated but finally I found your blog and I couldnt believe it can be that easy.
My sinciere thanks for your help.
Kind Regards,
Yudhvir Dahiya
i am displaying my wordpress posts on a site.and i have managed to display the latest post with all the comments. but the problem is that i cant display special chars on the site and spacing as they are on the wordpress install. on the wordpress site the special chars are coming properly. how do i display special chars properly outside the loop in the post as well as the comments?
i have included wp-config.php first then
then used the post loop and then called comments.php
but i cant figure out how to display the content of post and comments with proper spacing and the special chars??
So, I am having trouble with the info you spoke to Alex Colket on April 7th. I am trying to figure out the filesystem naming or location.
My wp-blog-header.php is in: /root/blog/wp-blog-header.php Here I use
require('./blog/wp-blog-header.php');The page I am putting this into is located at:
/root/folder1/folder2/folder3/page.php
I don’t know what the
require('/?/?/?/wp-blog-header.php')should be.any ideas??
I can’t seem top figure this out. I can get it to work on a php page in the root directory, but not the other one.
@fretbuzz: I hope that by “/root” you’re referring to the root directory of the site, not the home directory of the root user on the server.
Assuming this is the case, try this:
This will work from any directory on the site.
Thanks Kenn!
You’ve made my day with this article. Thank you!
Hi
I would like all of my pages to have the same post layout as the main page (I want to add as many posts as I can.
The same question as Nacy:
When I put the codes my page becomes ‘blank’.
Thank you.
@Jonathan, @Nancy: Just make sure the path to
wp-blog-header.phpis correct. See the information under the “Bonus” heading in this article.Note that this will only work if the WP site is on the same server and you have filesystem access to that site’s DocumentRoot (ie, no open_basedir restrictions, etc).
If you try this and get a blank page, check your Apache error log for PHP errors. These will usually point you in the right direction.
thanks… here what i found in my error log;
PHP Fatal error: Call to undefined function: query_posts()I think it’s a permissions issue.
What if I copy the
wp-blog-header.phpfrom my blog folder to my website folder?Kenn,
What a great, easy to follow tutorial. I think the fact that people are still leaving comments is a testament to how useful it is.
I do have a question for you – I followed your instructions and weaved some posts into my homepage. There are 9 posts total in my blog and on my homepage I’ve set ’showposts’ to 5 so that the page doesn’t run on and on. But after those 5 posts, I’ve love to just list the titles and dates of the remaining four posts, under a heading of ‘Recent Posts’ or something like that. Is that complicated? Does it require another loop? Thanks.
@Chad: Easy. Just add a counter variable inside your loop that increments each time, and then check for that when displaying the content.
Here’s an example of displaying the five latest posts and just headlines for five before that. This example is really stripped down but you can modify it however you need, adding HTML or additional WP template tags (eg, to show the date, etc).
I have one question. It works perfectly and my home page now shows the latest blog entries. How can I make it so there is a button that allows you to subscribe to the wordpress feed for my news entries(as you can see the feed button under the actual wordpress templates) from my home page?
And thank you SOOO much for this tutorial. It helped like you wouldn’t believe. I spend 2 weeks trying to find out how to do this to my current website with no experience in Mysql and you taught me that in minutes. Thanks.
You’re a good man Kenn Wilson. Thank you, thank you, thank you!
@Avneet: You can either use a
<link />element in your page header or a simple HTML link directly to the feed. This doesn’t depend on WP code at all.Thank You very much. I am saving your contact info. Eventually as our company grows we will need someone to build a new, better website and I see that person in you. You gave great help. Thanks again.
Is there a way to do this to also display WP categories on a sidebar as well as recent posts on an external site?
Sir, you are the best kind of gentleman.
The blog I’m building is completely outside the normal Wordpress theme as it seemed the best way to allow for mutliple pages, with different layouts each displaying a different category. however, I’m having problems including the actualy comments and form on these same pages, as I’m trying to totally cut out the theme system. Do you have any tips regarding this?
Cheers.
@Jonathan: Not really, no. I haven’t tried to pull that much functionality out of the WP framework so I don’t have any advice to offer. If you get it working, write up your experience and let me know!
I have a website that integrates wordpress into it with one login that occurs outside of wordpress. The code works well on my “localhost” computer at home, but when I move it to the production machine the login works but when I want to access the http://www.example.com/blog part of it, wordpress asks me to log in…..
1. is there an issue with cookies?
please take a look at the code below and let me know
Hey man this article is great!
I got it working in five minutes, great stuff, thanks a lot!!
But I also have a problem displaying content on other pages then the index.php
Domain structure is like example.com/blog/ but it seems only to work on my index.php? I tried it to add on other pages but it doesn’t do anything… its not even giving me errors.
Any ideas Kenn?
@Paul: If it works locally and not on the production server, the first thing you want to do is check for hard-coded hostnames. Make sure you don’t have “localhost” in your code anywhere. My article on portable Wordpress configuration files may help here.
If that doesn’t turn up a problem, start looking into configuration differences between the two environments.
@Devinia: There’s no reason this shouldn’t work on any page you put it on. If no errors are being output to the browser, you may have
display_errorsturned off in your PHP configuration. Enable this or check your server’s error log for problems.hey man, dont know if you check this anymore, but is there anyway of creating a picture for each post to link to it, as well as the excerpt and read more..
also how would you include a link to view comments for each post next to the posts on the site..
and finallyyy, how can you make on each page of the website have different posts from the blog by choosing which ones you want to be displayed…
I’d appreciate sooo much man..thank you!!
@Ross: The answer to your first two questions is “yes” but this is general Wordpress API stuff and you’d be best off consulting the documentation. Once you’ve included WP in the manner described here, you have full access to the API and can use whatever template tags you like.
You can include posts on multiple pages by applying this technique to all the pages you want your WP content on. Then adjust your
query_posts()call on each page to only retrieve the categories appropriate for that page.i am not too familiar with the documentation side of WP but i will have a look at it..see if i can figure anything out, not quite sure how to go about it..
thanks for the fast reply, your one of the first to actually reply!
cheers
This post was exactly what I was looking for. Couldn’t have asked for a better tutorial. Just want you to know I really appreciate it.
Craig
Hey,
for first, thank you for our great tutorial – helped a lot.
Anyway, I have a diacritic problem. You can see yourself. Any tip how to solve it? I’m using the same code in the head section as on the other parts of site.
Another piece of help would be much appreciated.
Live well. A.
Thanks allot been working with Wordpress on allot of my projects. finding this out has become a massive solution to a few problems.
Thanks Again!
Your comments are welcome
I'll help where I can. If you're really stuck, I'm available for consulting on an hourly or project basis. Get in touch for details.
Comment notes
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">