Include Basic WordPress Functions
<?php // Include WordPress define('WP_USE_THEMES', false); require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php'); query_posts('showposts=1'); ?>
<?php while (have_posts()): the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p> <?php endwhile; ?>
This can be used on any PHP file even OUTSIDE your WordPress installation.
I never seen this earlier when I submitted it…..
I only wish it was my idea to have this, if not…. Boo Hoo
Just noticed it could be mine with the class=”red”
just to let you guys know, you can make your own classes for the link, I added that for the site I used it on.
Remember you can change any of the HTML, just don’t change the PHP toooooo much
Hi, I’m, trying to use this example on my website but i’m getting this error.
Fatal error: Call to undefined method stdClass::set_prefix() in /home/../public_html/blog/wp-settings.php on line 268
I’m using the WP 2.8.4 and this example seems to be out of date, can anyone help me?
Thanks for your concern.
@Mauricio – I am getting this same error message and I have narrowed down the problem but I still don’t understand why the problem exists. Like the post says, I run this code in a PHP file outside of WordPress. If I include the PHP file directly from my site’s template, everything works fine and I don’t get the error. But if I include the file with a function (call a function that includes the file within the function) I get the fatal error.
Can anyone shed some light on this problem? This seems to be a very common problem according to Google.
That’s bad idea. Why don’t you just use
something went wrong…
…would probably work better than topic method.
@Mauricio, I received that oddball…
Fatal error: Call to undefined method stdClass::set_prefix()
…if I tried to do the…
…inside a function. Once I placed it at the top of the PHP page and not in a function, the problem was resolved. Note also you may have to use a virtual path to that file if you try to use it inside a plugin like I was. For instance, in a plugin, the path would be:
Thanks Mike, I managed to make it work by putting the code on top of the PHP page. So now I get a list of blog posts from WordPress, but now I need to make template for single post view work. I see how to integrate a list of excerpts, but how to integrate single view template? Anybody?
Try to add
It worked for me. And i hope i’ll help you too.
Thanks it worked …. I was looking for this code . wonderfully simple..
Thanks it’s really good. Can you help me to display page navigation (Numbering) at the bottom.
Really helpfull, works pretty nice! Good job, and thanks for share it :)
I have 2 blogs within the same database, and i’m running the loop in a ‘custom’ page outside of wordpress. Everything works fine for the first blog but im having trouble with the second blog. Im pretty sure the initial setup was done correctly (not by me) where the first blog has ‘wp_’ as table prefix, the second one has ‘wp2_’.
The problem arises when i try to pull the second blog into a ‘custom’ Loop. I basically have no idea how to do this.
I tried using `global $wpdb;$wpdb->set_prefix(‘wp2_’);` but it didnt work as intended.
I print_r’d $wpdb and indeed the prefix wp2_ is being appended in front of some of the tables, but I’m not really sure if it’s correct.
Does anyone have any idea?
Any idea on how to use this on mulitsite?
I would like to know the same thing. I would think you could do the same thing with MU
I want to get the single post page outside my wordpress folder.
Is it possible?
Nice, this is exactly what I was looking for. Worked perfectly. Make sure you guys are requiring the right way, using relative URLs.
That assumes your blog is on the same directory as the file that’s calling the require.
Ankur, this saved me! Just adding that full-stop before the slash!!
It was working without it on my local WAMP server!! But upon uploading to the remote server, things went kaput!
Is there a way to do this so that each post displays in a separate div so that I can style each one differently? I want to display the 3 latest posts in an external website each in their own box with a different background colour.
how to replace this line for my website?
One must not use “query_posts” (it is a requirement from WordPress). One must use pre_get_posts.
Hello, it is possible to embed this local code like this site made?
They use this script to embed the code and show latest post on other website:
You can try this script or see the print screen to show what they display: http://mafuba.ch/img/printscreen.png
Are they using the same techniques as yours?
Thank you in advance, and sorry for my english.
Your post notes that “This can be used on any PHP file even OUTSIDE your WordPress installation.”
This is not true is you are attempting to run the loop on a separate site/server.
For instance, I have a WordPress install @ http://www.example.com/blog. This code will work if I want to run the loop on http://www.example.com, but if I want to run the loop on http://www.otherwebsite.com it will not work, because you can not specify a URL in the server path.
Does anyone know of a solution for this particular issue?
I have a single blog that resides at http://www.example.com/blog and I want to feed recent posts to both http://www.example.com and http://www.otherwebsite.com.
I have managed to add recent posts to http://www.example.com using code similar similar to that posted here, but can not figure out how to implement it on a truly external site.
How can I add recent posts to http://www.otherwebsite.com in a similar fashion?
Try learn how to connect to the API that Worpdress provides you and use it to pull contect from one domain to your app on the other domain..