Support » Fixing WordPress » Example: WordPress Gallery Improvements

  • Hey everybody, I figure I’ll do the “How-To” part of the forum justice for a change. Been asking a lot of questions recently…

    I made changes to the WordPress gallery on my page, to make the galleries more useful and easier to navigate. Generally, I like the gallery that was introduced with v2.5 – the shortcode is a breeze to use, and captions etc. are nice. But on top of a few annoyances (like missing Category editing for images, and no inclusion in the page search), the gallery has a major drawback: if you use medium-sized images (which are displayed on their own page, with comments etc.), you get a standalone page that contains no linkback to the parent post – or the rest of the gallery. And if you have popular images that search engines link to, you get a lot of traffic that lands on your medium image page, clicks on the full version of that image, and leaves the blog because they don’t even know about the parent post or the other gallery images.

    So here’s an example of the changes that I made:

    Whenever you click on a gallery image, the page for the medium image displays a different sidebar with a thumbnail list of all images in that gallery. There’s also a link that refers back to the parent post.

    The code is still pretty damn ugly, and I have to hack around some limitations. But here’s the basic approach, which is quite simple:

    To get a linkback to the parent post, you do a query for parent of the current page, like so:

    global $post;
    if( $post->post_parent ) {
    $parent = wp_list_pages("title_li=&echo=0&hierarchical=0&include=" . $post->post_parent);
    $linkback = $post->post_parent . $parent; }

    (Credit to this thread for pointers). You can then construct a link that leads back to the post:

    echo "Go to the <a href=\"$linkback\">parent post for this gallery</a>.";

    This is semi-ugly because it doesn’t construct a Permalink to the post. But it works, and the Permalink solution can be added later.

    Now we need to add this to all pages that display a medium gallery image. I had to hack around what I think is a bug in the attachment templates. I can’t get attachement.php or image.php to work in almost all themes that I tried (but it does work in Kubrik!) – read my forum post here for info.
    So I modified single.php instead. The assumption is that whenever a post has a parent post, it is an image post (which is true for my blog, but might not for you). We already have this information from the check above, and can add a simple if statement to single.php:

    if ( $linkback ) {
    	echo "Go to the <a>parent post for this gallery</a>."; }

    In my case, I’m not adding this linkback to the body of single.php, I’m calling an include to a different sidebar instead. The above will be easier if you just use the image.php template – but as I said, it’s not working correctly for me yet.

    As for the actual thumbnail gallery in the sidebar, that code is a modified version of WordPress’ built-in gallery code. You can find it in wp-includes/media.php, starting at line 384 (in v2.6). Instead of relying on the extract(shortcode_atts function, I declare the parameters that are usually passed in (like $size = ‘thumbnail’;), and use the same find-the-parent-post code snippet as above to set the $id variable that refers to the gallery of the parent post. At the end, I don’t return $output, but echo it instead to post my generated list of thumbnails.

    Some work could be done here to exclude the image that user us currently looking at from the sidebar thumbnails, and of course the styling of the thumbnails could be changed to account for large numbers of thumbnails (I usually don’t post more than 6 images per gallery, so it works out well enough for me).

    Anyway, hope that helps. I’m happy with the way things are working now 🙂

Viewing 1 replies (of 1 total)
  • Fixed the non-Permalink issue, which was laughably easy to do using get_permalink(). So you just do

    $linkback2 = get_permalink($linkback);
    echo "<center>Go to the <a href=\"$linkback2\">parent post for this gallery</a>.</center><br />";

    Or sidestep the variable reassign by wrapping the get_permalink() function straight in the echo. Learning WordPress is fun 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Example: WordPress Gallery Improvements’ is closed to new replies.