在Image.php图库的末尾添加“更多图库”页面

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

在许多杂志网站上,当你点击一个图库(在WP中使用image.php模板完成)时,最后一次点击会将你带到一个“更多图库”页面。我想实现类似的目标,并希望通过为“更多画廊”页面制作模板来实现这一目标。问题是我不知道如何让我的下一个/上一个链接在最后一张图片上显示上一个链接,该链接会重定向到“更多画廊”页面,而不是另一张图片。

I figure the best way to do this would be to add a statement to the previous link which sends the user to a predefined link (the more galleries page) when the previous link no longer links to an image... but I\'m a little lost on how that would be done.

如果有人有更好的想法,我也在寻找其他可行的解决方法。

我的当前代码:

<?php
        $parent = $post->post_parent;
        $attachments = array_values(get_children(array(\'post_parent\' => $post->post_parent, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => \'ASC\', \'orderby\' => \'ID\') ) );
        foreach ( $attachments as $k => $attachment )
        if ( $attachment->ID == $post->ID )
          break;
        $next = $k+1;
        $prev = $k-1;
        $next_link = "";
        $prev_link = "";
        if ( isset($attachments[$next]) )
        {
          $next_exists = 1;
          $next_link = "<a href=\'" . get_attachment_link( $attachments[$next]->ID ) . "\'>←</a>";
          $img_link = "<a href=\'" . get_attachment_link( $attachments[$next]->ID ) . "\'>";
        }

        if ( isset($attachments[$prev]) )
        {
          $prev_exists = 1;
          $prev_link = "<a href=\'" . get_attachment_link( $attachments[$prev]->ID ) . "\'>→</a>";
          if  (!$next_exists)
          {
            $img_link = "<a href=\'" . get_attachment_link( $attachments[$prev]->ID ) . "\'>";
          }
        }
        if (!$next_exists and !$prev_exists)
        {
          $img_link = "<a href=\'" . get_attachment_link( $attachments[$k]->ID ) . "\'>";
        }
        $img_url = wp_get_attachment_url( $attachments[$k]->ID );
        if (sizeof($matches) > 1)
        {
          $fn = "/home/phasenet/public_html" . $matches[1];
          $exif_data = read_exif_data($fn);
        }
      ?>
当前如何调用链接:

<?php echo $prev_link ?>
<?php echo $next_link ?>

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

将此应用到您的功能中。php。您必须将$gallery\\u页面中的链接路径替换为指向更多库页面的链接。

<?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);
    }
    // This if statement has been added
    if (strpos( $nav_link, \'<a \') === false ) {
        $text = $text ? esc_attr($text) : \'\'; 
        $title = __(\'More galleries\', \'lwy_translate\');    // text which is displayed instead of Next/Previous 
        $gallery_page = \'/link_to/more/galleries_page\';    // replace with URL to the more galleries page
        printf(\'<a href="%1$s" title="%2$s">%3$s</a>\', $gallery_page, $title, $text);
    }
}
?>
如果使用theme Twenty Eleven,则模板文件图像中的上一个/下一个链接。php应该如下所示

<span class="nav-previous"><?php lwy_previous_image_link( false, __( \'&larr; Previous\' , \'twentyeleven\' ) ); ?></span>
<span class="nav-next"><?php lwy_next_image_link( false, __( \'Next &rarr;\' , \'twentyeleven\' ) ); ?></span>
如果不使用主题“twentyeleven”,请将“twentyeleven”替换为主题/子主题翻译文件的章节名称。由于文本“More galleries”可能不存在于任何主题翻译文件中,因此您必须自己创建一个以支持其翻译。否则,将以所有语言显示英文版本。

由于此代码部分使用了来自WP源的代码,因此应使用新版本的WP审查其更改。

EDITED 23.12.2011这是函数lwy\\u nextant\\u image\\u link()中最后一部分的改进版本。不要忘记用实际链接替换“更多库”页面的链接。

<?php
    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;
?>

结束

相关推荐

User-based media gallery

我正在建立一个同时使用WordPress和BuddyPress的网站,我希望建立一个自定义部分,用户可以上传作品,人们可以对其进行评论/评分。我浏览了各种不同的选项,但似乎使用自定义的帖子类型,然后允许成员提交内容是最好的选择。这是更好的方法吗?还是你认为帖子类型是最好的选择。要了解其所需的功能类型,请执行以下操作:http://dribbble.com/其想法是将图像作为类似域的URL。com/designs/my-awesome-work-2201/当查看成员档案时,您将能够查看他们的最新作品等,因此