Gallery Inside One Post

时间:2013-01-24 作者:AndrettiMilas

对于图像库,我需要一个使用下一个/上一个链接的代码,将您带到下一个/上一个图像in that postkeeps the images in the correct order you chose while inserting them into the post. (每个主要新闻网站似乎都有这一功能。)

当前,如果您添加<?php next_image_link( false, \'Next Image\' ); ?><?php previous_image_link( false, \'Previous Image\' ); ?> 符合您的形象。php或WordPress中的附件模板the links may bring you to a gallery in a different post if the image is used elsewhere.

为了进一步说明这个问题,take a look at this gallery. 有三个图像。如果您单击企鹅并开始使用上一个/下一个链接滚动浏览它们,您将看到不应显示的其他图像!

This problem still has been solved. I had to use the media editor to attach the posts... too bad WP isn\'t always doing this natively when the image is uploaded.

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

这些功能将有助于:

//function to get next or previous keys in an array
function array_navigate($array, $key){
    $keys = array_keys($array);
    $index = array_flip($keys);
    $return = array();
    $return[\'prev\'] = (isset($keys[$index[$key]-1])) ? $keys[$index[$key]-1] : end($keys);
    $return[\'next\'] = (isset($keys[$index[$key]+1])) ? $keys[$index[$key]+1] : current($keys);
    return $return;
}

function previous_attachment_ID($att_post){
    //get the attachments which share the same post parent
    $images =& get_children(\'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=\'.$att_post->post_parent);
    if($images){
        //get the id of the previous attachment
        $ppid_arr = array_navigate($images, $att_post->ID);
        $ppid = $ppid_arr[\'prev\'];
        return $ppid;
    }
    return false;
}

//previous attachment link function
function prev_att_link($att_post=null){
    if($att_post == null){
        global $post;
        $att_post = get_post($post);
    }
    $ppid = previous_attachment_ID($att_post);
    if($ppid != false){
        return \'<a href="\' . get_attachment_link($ppid) . \'" class="previous-attachment-link">Previous</a>\';
    } else {
        //there is no previous link
        return false;
    }
}

function next_attachment_ID($att_post){
    //get the attachments which share the same post parent
    $images =& get_children(\'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=\'.$att_post->post_parent);
    if($images){
        //get the id of the next attachment
        $ppid_arr = array_navigate($images, $att_post->ID);
        $ppid = $ppid_arr[\'next\'];
        return $ppid;
    }
    return false;
}

//next attachment link function
function next_att_link($att_post=null){
    if($att_post == null){
        global $post;
        $att_post = get_post($post);
    }
    $ppid = next_attachment_ID($att_post);
    if($ppid != false){
        return \'<a href="\' . get_attachment_link($ppid) . \'" class="next-attachment-link">Next</a>\';
    } else{
        return false;
    }
}

//back to gallery link function
function back_to_gal_link($text="Back to gallery"){
    global $post;
    $post = get_post($post);
    $post_par = get_post($post->post_parent);
    $slug = get_permalink($post_par->ID);
    return \'<a href="\'.$slug.\'">\'.$text.\'</a>\';
}


//show all thumbnails function
function show_all_thumbs() {
    global $post;
    $post = get_post($post);
    $images =& get_children( \'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=\'.$post->post_parent);
    if($images){
        foreach( $images as $imageID => $imagePost ){
            if($imageID==$post->ID){
            } else {
                unset($the_b_img);
                $the_b_img = wp_get_attachment_image($imageID, \'thumbnail\', false);
                $thumblist .= \'<a href="\'.get_attachment_link($imageID).\'">\'.$the_b_img.\'</a>\';
            }
        }
    }
    return $thumblist;
}
有了它,您可以根据需要实现上一个下一个设置,并且可以在库中显示所有缩略图。

e、 g.:

<div class="back_to_gal_link">
    <?php
    echo back_to_gal_link().\'<br />\';
    ?>
</div>
<div class="prev_next_links">
    <?php
    if(prev_att_link()){
        echo prev_att_link();
    }
    if(prev_att_link() && next_att_link()){
    //insert a seperator between the two links
        echo \' | \';
    }
    if(next_att_link()){
        echo next_att_link();
    }
    ?>
</div>
<div class="thumbnails">
    <?php
    echo show_all_thumbs();
    ?>
</div>

结束

相关推荐

Fullscreen gallery plugin

我一直在寻找wordpress全屏图库,它支持WP核心图库功能,不基于flash。它应该类似于mashable。com(即。http://mashable.com/2012/03/03/instagram-products-decor/) 或