Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When I add lines 3 - 19 to the top of functions.php, then try to update image.php I get the error "Warning: Cannot modify header information - headers already sent" when I try to update my image.php.

The code on lines 3 - 19 creates custom next/previous links for my image gallery and redirets the last one to a "more galleries" page.

My code can be seen here: http://pastebin.com/Yen04t2z

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I've seen in the pastebin the problem and merged the whole code together again. Put this first part completely into your functions.php.

/**
 * Display previous image link that has the same post parent.
 *
 * @since 2.5.0
 * Original version in /wp-include/media.php
 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
 * @param string $text Optional, default is false. If included, link will reflect $text variable.
 * @return string HTML content.
 */
function lwy_previous_image_link($size = 'thumbnail', $text = false) {
    lwy_adjacent_image_link(true, $size, $text);
}

/**
 * Display next image link that has the same post parent.
 *
 * @since 2.5.0
 * Original version in /wp-include/media.php
 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
 * @param string $text Optional, default is false. If included, link will reflect $text variable.
 * @return string HTML content.
 */
function lwy_next_image_link($size = 'thumbnail', $text = false) {
    lwy_adjacent_image_link(false, $size, $text);
}

/**
 * Display next or previous image link that has the same post parent.
 *
 * Retrieves the current attachment object from the $post global.
 *
 * @since 2.5.0
 * Original version in /wp-include/media.php
 *
 * @param bool $prev Optional. Default is true to display previous link, false for next.
 */
function lwy_adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
    global $post;
    $post = get_post($post);
    $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));

    foreach ( $attachments as $k => $attachment )
        if ( $attachment->ID == $post->ID )
            break;

    $k = $prev ? $k - 1 : $k + 1;

    if ( isset($attachments[$k]) ) {
        $nav_link = wp_get_attachment_link($attachments[$k]->ID, $size, true, false, $text);  // save in $nav_link instead of displaying with 'echo'
    }
    // This if statement has been added
    if (empty($nav_link) || $nav_link == __('Missing Attachment')) {
        // If a new version of WP has been released, check this code snippet to behave equivalent to function wp_get_attachment_link() 
        // in post-template.php. Also the string 'Missing Attachment' has to be checked for changes.

        // $text  = $text ? esc_attr($text) : '';                   // this is simply the text which you set when calling lwy_previous_image_link() / lwy_next_image_link()
        $text = __('More Galleries', 'lwy_translate');              // display a fixed text like 'More Galleries'
        $title = __('Go to more galleries page', 'lwy_translate');  // the text which does popup when mouseover the link
        $gallery_page = '/link_to/more/galleries_page';             // !!! REPLACE WITH THE URL TO THE MORE GALLERIES PAGE !!!

        $nav_link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $gallery_page, $title, $text);
        $nav_link = apply_filters( 'wp_get_attachment_link', $nav_link, $attachments[$k]->ID, $size, true, false, $text );
    }
    echo $nav_link;
}

You have to replace the link in $gallery_page with the link to your more galleries page.

If you don't need translation add in image.php

<?php lwy_previous_image_link( false, '&larr; Previous'); ?>
<?php lwy_next_image_link( false, 'Next &rarr;'); ?>

You may modify the strings '← Previous' and 'Next →' as you like.

If translation should possible somewhere in the future add in image.php

<?php lwy_previous_image_link( false, __( '&larr; Previous' , 'lwy_translate' ) ); ?>
<?php lwy_next_image_link( false, __( 'Next &rarr;' , 'lwy_translate' ) ); ?>
share|improve this answer

Lines 3-19 are not placed within a function, so that code is being executed when functions.php is loaded. You should put your code in a function, and call the function at the appropriate time within a template, or use your function as a callback for an action or filter.

share|improve this answer
    
But would that actually fix the warning i'm getting? This is for a next/previous link in an image gallery, I assumed it is best to leave those simply in the functions.php and not necessarily in a function. –  AndrettiMilas Dec 24 '11 at 0:22
    
Yeah, it should. Don't echo stuff in functions.php outside of a function. That's just asking for headers already sent warnings. Output is being generated by the code at lines 3-19. This will be happening early in the flow of things. –  goto10 Dec 24 '11 at 0:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.