30 Oct, 2009
Multiple Featured Content Galleries
Posted by: Jennifer In: WordPress|WordPress Hacks|WordPress Plugins
On a site I was working on recently, the client was using the Featured Content Galleries plugin for their homepage (you can download it from here) – but they wanted to add a gallery like this to other pages on their website and have other featured content on those pages. This is not a feature currently supported "out of the box" with this plugin. But hacking it in is pretty easy.
First – you will need to create a category that you will use for the other Featured Content Galleries – one per page you want to use it with. Assign posts you want to appear in the featured content gallery with this new category. For example – lets say on your homepage you're running the featured content gallery, and it's showing posts from the "featured-posts" category. But lets say you have a "products" page and on that page, you only want articles related to your products showing up on that page. So you would need to create another category – lets call it "featured-posts-about-products". Then assign this new category to your articles about products. (**Make a note of what the category ID is of this new category!)
You will still need to make sure those posts have a custom field with a key of "articleimg" and a value that is the full URL to the image you want in the featured content gallery.
Now we can start hacking at the plugin…
Open up gallery.php (found in the featured-content-gallery plugin directory). MAKE A BACKUP OF GALLERY.PHP JUST IN CASE! Then, look for this line:
query_posts('category_name=' . get_option('gallery-category') . '&showposts=' . get_option('gallery-items'));
and replace with the following:
$categoryToUse = 'category_name=' . get_option('gallery-category'); if (isset($catidforgallery) && is_numeric($catidforgallery)) { $categoryToUse = 'cat=' . $catidforgallery; } query_posts($categoryToUse . '&showposts=' . get_option('gallery-items'));
NOW in your theme – where you have the featured content gallery appearing – if you want it to pull from that special category, you can do this:
<?php $catidforgallery = 1234; include (ABSPATH . '/wp-content/plugins/featured-content-gallery/gallery.php'); ?>
The above code assumes our category ID is "1234".
But lets say you're using this template for a number of different pages, and on each one you want a different content gallery… Again, set up these new categories, make notes of what their IDs are – then on each page that's using this template, to specify what category ID the featured content gallery should use, add a custom field with a key of "category-feature-rotator" and a value of the category ID you want in the gallery.
Modify the theme/template code to this instead:
<?php $category_feature_rotator = get_post_meta($post->ID, "category-feature-rotator", true); $catidforgallery = false; if (isset($category_feature_rotator) && $category_feature_rotator != 0) { $catidforgallery = $category_feature_rotator; } include (ABSPATH . '/wp-content/plugins/featured-content-gallery/gallery.php'); ?>
That will look for the custom field and use the category ID you put in for that custom field – and if it's not there – then it will just default to the category specified on the Featured Content Gallery options page.
IMPORTANT NOTE: If/when there is an update to the Feature Content Gallery plugin – upgrading will overwrite these changes. This post was written for version 3.2.0 of the plugin. Hopefully they'll build this feature in a next release of the plugin. 😀
UPDATE 11/16/09 Greg had asked in the comments about being able to do a similar technique with multiple featured content galleries, but being able to specify page IDs. He was able to solve the problem before I could and sent me the solution.
To use Page IDs in the gallery: On the page you want to have the gallery displayed on, create a custom field with a key of "pageids" – and for the value, give a comma seperated list of page ids.
Where I had been pulling the "category-feature-rotator" custom field – replace that with the following:
$pageids = get_post_meta($post->ID, "pageids", true);
Then look for the following line in gallery.php (should be around 24)
$arr = split(",",get_option('gallery-items-pages'));
and change it to this:
$arr = split(",",($pageids));
*Note: I haven't personally tested the Page ID method – so if you try it – please report back how it goes…