So a friend of mine shoots me an email today. She had a front end website, just set of basic pages built with PHP and one of the links on the navigation was a WordPress blog installed on the same hosting. She wanted to display an RSS feed in the sidebar of the front end website by using a nifty little script called Simple Pie.
Looking over the script, I found it to be a handy little script. It pulls an RSS feed and parses it easily for you and then you can output the parts you want with links to the full article. The only problem I found with what she was trying to do, she just wanted to parse the RSS feed of the WordPress blog on the same site and display the last 5 articles on the home page of the primary site.
This is easily done with about 8 lines of code. You can get full access to everything WordPress has to offer from external PHP pages on the same site.
The first thing you need to do is include a WordPress file in order to load the functions you’ll need access too:
<?php
require_once('./blog/wp-blog-header.php');
?>
The above code is assuming that your installation is in the “blog” directory, naturally change it to reflect your installation. Once this is included in your script you can access all the WordPress functions. Now if we expand on that code as follows:
<?php
require_once('./blog/wp-blog-header.php');
query_posts('showposts=5'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
Now if you plug through the code you’ll see that we are pulling the last 5 posts (showposts=5) and then we are looping through that information and writing it in HTML. Surround each title with an H2, or your own choice, and create a link with the appropriate WordPress permalink.
So instead of messing with an entire separate class or script we’ve included the WordPress functions, then just used the functions to display the information we already wanted. Keep it simple. Use the tools you already have available to you.
The main lesson I find with this example is that there is always 101 ways to skin a cat. The idea is to make sure you are using the tools already available to you, and implementing the solution that keeps it simple. Don’t get focused in on a solution and waste a lot of time, and never hesitate to ask for help.
Related posts:
{ 34 comments… read them below or add one }
Hi Ed,
Thank you very much for providing the article. I know this post is rather old, but I hope you might be able to help me out with implementing the script you’ve outlined. I’m not having any success with the require_once() method, and I am not sure what the problem is, since it seems so straightforward.
At the moment, I’m using JQuery to make an Ajax call to “custom_functions.php”. I don’t want to add the new function definitions directly to the theme’s original “functions.php” because I need to pass specific information asynchronously and have the server return new DOM elements based on the information given it.
The error I see is:
“Fatal error: Call to undefined function query_posts() in /..filepath../custom_functions.php on line 23″
I know that the line “require_once(‘./filepath…/blog/wp-blog-header.php’);” is being executed correctly, since there is no fatal error caused by the require_once() method (I kept getting those until I correctly inputted the link). At least I THINK it’s safe to assume that the wp-blog-header.php is being imported into the new document.
Do you have any ideas on how I can fix this? I am at a complete loss. Any help would be greatly appreciated!
Could you tell me a little more about the JQuery/AJAX call you are making to the Custom_functions.php file?
There are several references on the web related to the require_once() implementation having some issues. You will notice the solution in my post is all self contained. You may have an issue with Wordpress Globals not being passed outside the active function.
Here is one such reference: Calling wp-blog-header.php from inside a PHP function
just thought I’d add my 2 cents!
On the site I am currently working on, I am using WordPress as the CMS….
Backend (wordpress):
-login as admin
-add categories for your page, and page sections
(i.e. home, home-top,home-top-left,etc…wherever you want controllable content)
-add content(posts) to your categories
–posts->add new
—title->something distinctive(Home: top content)
—category(select the category you want the article to belong to (home-top))
—write article
—-publish!
Frontend (the website):
- at the top of pages that use wp blog entries
(
)
-here is the code you place in whatever section
( div ‘home-top’ being the container id same as the post category for readability…can be any id, or container for that matter)
Basically I am able to control every section of my site through word press (content wise) and by using the user permissions module I am also able to use the blog capabilities as well by hiding all the section content from all but admin…anyway I thought it was cool, peace out.
When I follow your 8 lines of code and use them strangely my browser get redirected to the wp-admin/install.php page…
When using the code above you should add header(“HTTP/1.0 200 OK”); after your require otherwise it’s sending a 404 (although the content is still loading) which Google doesn’t like. Also if you integrate the header in something like JSON, a 404 header prevents it from rendering properly.
I wear a necklace, cause I wanna know when
I’m upside down.
Here’s the latest on the conference’s top players who are projected to go from playing on Saturdays to starring on Sundays.
1. LB Jarvis Jones, 6-3, 241, Georgia
Why he has the top spot: Jones’ battle with an ankle injury hasn’t changed his status as the top overall draft prospect in the SEC. He missed his second full game due to the
injury in the Bulldogs’ 29-24 win over Kentucky on Saturday. Still, Jones remains a versatile defender who can rush the pass or drop into coverage. He’ll need a strong second
half of the season to maintain his lofty draft status.
Season stats (five games): 36 tackles, 5.5 sacks, 9.5 TFLs, 3 forced fumbles, 15 QB hurries, INT.
Player ranking: Jones remains fifth overall, and he is the second-highest outside linebacker in CBSSports.com’s latest player rankings.
Up next: The Bulldogs’ next game is against Florida. Jones returned to practice on Monday and is expected to play. Georgia will need him against a Florida team that averages 213
yards per game on the ground.
2. QB Tyler Wilson, 6-3, 220, Arkansas
Why he has the second spot: After a slow start for Wilson and the Razorbacks, the senior QB has turned it around in the last few weeks before having a bye last weekend. He’ll
have a chance to solidify his status as a first-round pick with games against Mississippi State, LSU and South Carolina still on the schedule.
Season stats (six games): 124 of 205 (60.5 percent) for 1,957 yards, 14 TDs, 5 INTs.
Player ranking: Wilson is ranked as the No. 12 overall prospect and is the third-highest ranked QB in CBSSports.com’s latest player rankings.
Up next: The Razorbacks play Ole Miss this weekend. The Rebels are ranked No. 65 nationally in scoring defense, so Wilson should have a chance to put some good numbers up.
NBA JERSEYS
Ahaa, its good discussion on the topic of this article here at this webpage,
I have read all that, so now me also commenting here.
Greetings! I’ve been reading your weblog for a while now and finally got the bravery to go ahead and give you a shout out from New Caney Texas! Just wanted to tell you keep up the good job!
Wonderful items from you, man. I have consider your stuff previous
to and you’re simply too excellent. I actually like what you have obtained here, really like what you’re saying and the
way in which wherein you assert it. You’re making it enjoyable and you still care for to keep it wise. I can not wait to read much more from you. That is actually a great website.
In fact, despite what a lot of people think, there isn’t such thing as being a “pure” soy pillar candle. You could be tempted to buy cheap scented soy candles because of these price, however you’ll only
be wasting your money and you also won’t truly enjoy everything you’ve bought.
The standard wire-core wick can provide with soy candles.
The other day, while I was at work, my sister stole my apple ipad and tested
to see if it can survive a 30 foot drop, just so she
can be a youtube sensation. My apple ipad is now destroyed
and she has 83 views. I know this is totally off topic but I had
to share it with someone!
Hello to all, because I am actually eager of reading this weblog’s post to be updated on a regular basis. It carries good stuff.
Thanks for your marvelous posting! I quite enjoyed reading it, you might be a great author.
I will ensure that I bookmark your blog and will
often come back at some point. I want to encourage continue your great writing, have
a nice day!
I’m not that much of a internet reader to
be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your site to come back later. Cheers
Wow, wonderful blog layout! How long have you been blogging
for? you make blogging look easy. The overall look of your web site is excellent, as well
as the content!
Professional locksmiths can be found round the clock for your locksmith
needs. In a hectic parking spot, the smart key might help to recognize the location with the car developing a distinct sound with
flashing of lights by the click. With all the important roles played with
a locksmith, it’s high time for it to regard them as key players inside the community.
You’ve made some decent points there. I looked on the internet to learn more
about the issue and found most people will go along with your views on this website.
I just like the valuable information you supply on your articles.
I’ll bookmark your weblog and test again here regularly.
I’m rather certain I will be informed many new stuff right right here!
Good luck for the next!
A bathroom that looks good and functions well is a fantastic place to be
in. Expect to spend at the least $10,000 when you choose brand new low-end cabinetry, counters, appliances, flooring,
paint, and a few minimal structural alterations. Other technological advancements include the development of sensors
that allow buildings to know the strength of tremors and respond appropriately, including the activation of
tremor or movement damper systems that act like shock
absorbers, letting buildings withstand stronger tremors without collapsing.
Je termineraі ɗe regarder tout cela plus tard
Très bbon poste, j’espèгe en parler demain avec mes
amis
Il me tardе de lire le prochain post
Voսs nous concoctez continuellement des postes ρassionnants
Je sսis pressé de liree le prochain рoste
At this time I am ready to do my breakfast, after having my breakfast
coming over again to read more news.
C’est unn vrai plaisir de visiter votre post
I wrote down your blog in my bookmark. I hope that it somehow did not fall and continues to be a great place for reading texts.
Je suis clairement du même avis que toi
Est-ilpossible de emprunter deux trois lignes pour mmon site internet personnel ?
Un article plein de conseils
Vous écrivez toujours des articles fascinants
On voit immédiatement que vous maîtrisez superbement bien ce sujet
C’est bizarre je pensais écrire un petit article semblable à celui là
{ 1 trackback }