I have two WordPress installations, one at http://inversekarma.in and the other at http://inversekarma.in/photos. The latter is a photoblog, and its theme uses the standard WP post thumbnails (EDIT: featured images, to be precise!). Is there a way to show the most recent thumbnails from the second site on my first site’s sidebar?
Leave a Reply
You must be logged in to post a comment.
There are (at least) two (2) ways to approach this:
You can query the second database which will require you to maintain the database credentials in two places, or at least in an include file, or
You could create a simple JSON feed for your photo blog and consume the feed on your main blog and cache it for a short time. This will cause a slight latency on page load when the cache expires but since both are on the same machine it shouldn’t be an issue.
Let me know which you’d prefer and if I’ll update the answer to so you how.
UPDATE
To connect to another database you can read more about it here:
So here’s what I hacked together for you, a function called
show_thumbnails_2nd_db()
which you can copy into your theme’sfunctions.php
file:Note: That the above assumes you have created a
database-credentials.php
in the root of your photos blog, and it should look like this:Then inside the
/wp-config.php
for your photos blog you would replace the sections of code that look like this:And this:
With this:
If you need more explanation just ask in the comments.
Mike’s answer is great, but I would use a different SQL query, since the GUID of the attachment post can change when you change servers or move the blog. My version also gives you access to information of the blog post, which is useful if you want to link to it.
Instead of
_wp_attached_file
you can also get the_wp_attachment_metadata
value, which contains extra info about the image and alternative sizes.