Display Wordpress content from Outside Wordpress
Ever wanted to call Wordpress functions from outside the Wordpress installation?, no plugin required, just a few lines of code.
Please note, this is an old article suitable for WP1/2, a new version is being built. In the meantime look at replacing wp-config.php
with wp-load.php
for WP 2.5 onwards.
wp-config.php
with wp-load.php
for WP 2.5 onwards.
Using this snippet you can list post titles, even excerpts or comments outside of Wordpress on your website without having to go through the web interface.
It’s as simple as creating a new PHP file. ie. newest.php in your Wordpress root (topmost folder) and then including it in any other file on your website that is outside of Wordpress.
- Firstly create a new file called: newest.php and place it in the WordPress root folder, here is the code for newest.php:
<?php $limit=5; $more=1; $count=1; if (empty($wp)) {require_once('wp-config.php'); wp('feed=rss');} if ($posts) { foreach ($posts as $post) { start_wp(); ?> <ol> <li> <div class="small"> <?php the_time('l, F jS, Y') ?></div> <?php the_excerpt() ?> </li> </ul> <?php $count++; if ($count > $limit) { break; } } } ?>
-
Use the following code outside Wordpress, in a static Index page, sidebar, header - simply wherever you want the Wordpress content to be shown:
<? @include ($_SERVER['DOCUMENT_ROOT'].'/wordpress/newest.php'); ?>
- Sorry, no more steps, that’s it your done!
That will list the latest post titles (default 5), display Date of Post, and an excerpt of the post.
thx for this little tutorial! One digg from me
Go to Top of the page
I Digg,too! Dugg? Thanks for this!
Go to Top of the page
simple yet mighty,
Thanks for this…
Go to Top of the page
Can I get to another website?
Go to Top of the page
@Alexdaklak
You would have to have access to the aforementioned files on the other website.
However you could just hook up into their RSS feed, like so:
please note you should replace [ ] with <> respectively.
Hope this helps
Go to Top of the page
This looks good {except for the specifics of calling wp()}, but when I try it on a clean 2.6.3 I get a fatal error inside wp-settings.php that a member function is being called on a non-object. It’s talking about the $wpdb object calling set_prefix. I can’t see why there would be a problem since WP works in every other way. Two lines up it checks !empty($wpdb->error without complaint.
What am I missing?
Go to Top of the page
I finally got back to trying this out. Here’s’ the error I get:
Strict Notice: Assigning the return value of new by reference is deprecated in wp-settings.php on line 472
My server says it has a PHP version of 4.4.8, but I thought it was higher since I’m able to run Magento. (Oh yeah, there’s this in the .htaccess file: AddHandler application/x-httpd-php5 php).
So is it just my version of PHP that has gone beyond allowing what WordPress is doing (assigning the return value of new by reference)? I guess I can hack the WP settings file not to do it that way…or turn off Strict… maybe?
Go to Top of the page
So is it possible to pull latest posts from different categories on to different pages of a “traditional” separate website using this technique?
i.e posts in “news” category to a news page
and post from “offers” category to an offers page.
Go to Top of the page
Is it possible to display comment form and comments from a specific post on a page outside of wordpress? I would like to show post comments on a page that is linked to inside the post but the link is on a page outside of wordpress. Would like to give option of commenting/reading comments in wp and on linked page.
Go to Top of the page
This looks good
Martino
Go to Top of the page
Problem: the_excerpt() doesn’t seem to work with posts that have permalinks. This is indicated in the wordpress codex, and sure enough, I can’t get it to work. Bummer
Go to Top of the page
In regards to your answer to Alexdaklak’s question: what is “have access” imply? For instance I have several sites hosted together including one with WP and another I’d like to include some of that WP content on using this method. Is there an additional step to load the necessary files or something in the server settings that has to be manipulated?
Thanks, much. I just much prefer the customization options of a regular include then running it through RSS…
Go to Top of the page
I have tried both of your methods but neither one of them worked
Is it still valid with the WP3.0 update?
Go to Top of the page
***This is an old article; when I have more time I will build a new version for later WP versions.
In the meantime, try swapping wp-config.php for wp-load.php, which was new for WP2.5 onwards.
Go to Top of the page
AweSome !!!
Go to Top of the page
i have used newest.php and it displayed the content but what i want more is need to add pagination to navigate older content. can anyone help me to do that? i tried but i am not able to do that.
Thanks in advance
Go to Top of the page
Thanks
Go to Top of the page
when i tried this code for for one blog it’s work great but in multiple blog this code is giving same output as 1st blog.
why this happen
Go to Top of the page