APR 26

Adding text links to WordPress Gallery

Posted on April 26, 2008 
Filed Under Tutorial

Please see this plugin before you read this article

Another WordPress tutorial!!!
Andy from Tokyo, Japan writes in asking:

Could you please tell me how you added the text navigation links to your gallery? I would be grateful… …Thank you.

Sure Andy, No problem, but first a little bit of background for everyone else:

Gallery Shortcode

WordPress version 2.5 added an exciting and very awesome feature: The Gallery Shortcode. This feature allows blog authors to add a gallery of thumbnails to their blog posts. Each one of these thumbnails is linked to an individual page which displays a larger version of the image and any other information that has been associated with that image (title, description, comments, etc…). A new template file was added to the WordPress Default theme to display single images. This template file is called image.php.

Image.php?

If your current theme does not have an image.php file, I would suggest that you visit your theme’s homepage and see if a new version has been released for 2.5+. If no new version is available, you can save a copy of the WordPress default theme’s image.php file to your current theme’s directory and modify it match your current theme.

Changing to Text Based Navigation

Now that everyone has a working image.php file in their current theme’s directory, it’s time get rid of those clunky thumbnail links! Unfortunately, I was not able to find any template tags which offered text-based links, so I had to create my own:

function mf_previous_image_link( $link_text ) {
    print mf_adjacent_image_link( $link_text, true );
}
 
function mf_next_image_link( $link_text ) {
    print mf_adjacent_image_link( $link_text, false );
}
 
function mf_adjacent_image_link( $link_text, $prev = true ) {
    global $post;
    $post = get_post($post);
    $attachments = array_values(get_children("post_parent=$post->post_parent&post_type=attachment&post_mime_type=image&orderby=\"menu_order ASC, ID ASC\""));
 
    foreach ( $attachments as $k => $attachment )
        if ( $attachment->ID == $post->ID )
            break;
 
    $k = $prev ? $k - 1 : $k + 1;
 
    if ( isset($attachments[$k]) )
        return '<a href="' . get_attachment_link( $attachments[$k]->ID ) . '">' . $link_text . '</a>';
	else
		return false;
}

You’ll want to paste the above code into your theme’s functions.php file. If your theme does not have a functions.php file, you can make one from scratch. Basically, I have copied a few functions from a core wordpress file, gave each one a unique name and modified them to suite my needs.

The next step is to locate the following code in your image.php file:

<div class="navigation">
	<div class="alignleft"><?php previous_image_link() ?></div>
	<div class="alignright"><?php next_image_link() ?></div>
</div>

Change the above code to:

<div class="navigation">
	<div class="alignleft"><?php mf_previous_image_link( 'Previous Image' ) ?></div>
	<div class="alignright"><?php mf_next_image_link( 'Next Image' ) ?></div>
</div>

Save both image.php and functions.php and upload them to your server. You should now have text-based navigation links for all of your WordPress Galleries.

Image Page Template Hierarchy

The list below illustrates the logic WordPress uses to determine which template file will display the the larger images in your theme. Basically, WordPress first looks for image.php if it is found, it is used, if not, attachment.php is used. Wordpress will try to locate each file in the list before it defaults to index.php.

  1. image.php
  2. attachment.php
  3. single.php
  4. index.php

In Conclusion

I hope this helps you out Andy (and whoever else might be reading). Feel free to leave questions in the comments section. Thanks for visiting!

APR 2

How to Activate Excerpts for Pages in Wordpress Admin Panel

Posted on April 2, 2008 
Filed Under Tutorial

I always wondered why wordpress allowed excerpts for posts and not for pages. A bit odd considering that both posts and pages are stored in the same database table and can have the same values stored for them. I have found a rather nice work around for this in 2.3.3 and then came…….. Wordpress 2.5!

I recently had a chance to upgrade one of the sites that I manage to the brand new Wordpress 2.5. I noticed right away that drastic changes had been made to the admin section - some for the better and some for the worse. The Requests and Feedback section of the Wordpress forums can better fill you in on all of this.

One thing I noticed immediately was that the Post and Page screens had been completely overhauled. This made me wonder whether or not my custom mf_add_excerpt_to_page() function still worked.

No dice.

Broken.

Luckily it took under 10 minutes to upgrade the function to work with two-point-five. For your viewing pleasure (drum roll please? ) here are both versions:

Wordpress 2.3

<?php
 
add_action( 'dbx_page_advanced', 'mf_add_excerpt_to_page' );
 
function mf_add_excerpt_to_page(){
	global $post;
?>
	<div class="dbx-b-ox-wrapper">
	<fieldset id="postexcerpt" class="dbx-box">
	<div class="dbx-h-andle-wrapper">
	<h3 class="dbx-handle"><?php _e('Optional Excerpt') ?></h3>
	</div>
	<div class="dbx-c-ontent-wrapper">
	<div class="dbx-content"><textarea rows="20" cols="40" name="excerpt" tabindex="6" id="excerpt"><?php echo $post->post_excerpt ?></textarea></div>
	</div>
	</fieldset>
	</div>
<?php
}
?>

Wordpress 2.5

<?php
 
add_action('edit_page_form', 'mf_add_excerpt_to_page' );
 
function mf_add_excerpt_to_page(){
	global $post;
?>
	<div id="postexcerpt" class="postbox <?php echo postbox_classes('postexcerpt', 'post'); ?>">
	<h3><?php _e('Excerpt') ?></h3>
	<div class="inside"><textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt"><?php echo $post->post_excerpt ?></textarea>
	<p><?php _e('Excerpts are optional hand-crafted summaries of your content. You can <a href="http://codex.wordpress.org/Template_Tags/the_excerpt" target="_blank">use them in your template</a>'); ?></p>
	</div>
	</div>
<?php
}
?>

You can enter each function/hook combination into your theme’s functions.php file.

Hope someone finds this useful.