Functions.php Problem

时间:2011-12-24 作者:AndrettiMilas

当我在函数顶部添加第3-19行时。php,然后尝试更新图像。phpI get the error "Warning: Cannot modify header information - headers already sent" when I try to update my image.php.

第3-19行的代码为“我的图像库”创建自定义的下一个/上一个链接,并将最后一个链接重定向到“更多库”页面。

我的代码可以在这里看到:http://pastebin.com/Yen04t2z

2 个回复
最合适的回答,由SO网友:jot 整理而成

我在粘贴箱中看到了这个问题,并再次将整个代码合并在一起。将第一部分完全纳入您的功能中。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;
}
您必须将$gallery\\u页面中的链接替换为指向更多库页面的链接。

如果您不需要翻译插件图像。php

<?php lwy_previous_image_link( false, \'&larr; Previous\'); ?>
<?php lwy_next_image_link( false, \'Next &rarr;\'); ?>
您可以修改字符串(&L);“上一个”和“下一个”随你的便。

如果翻译应该可能在未来某处添加图像。php

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

SO网友:Dave Romsey

第3-19行不在函数中,因此在函数运行时执行代码。php已加载。您应该将代码放入函数中,并在模板中的适当时间调用该函数,或者将函数用作操作或筛选器的回调。

结束