問題描述
對於圖像相冊,我需要一個代碼,使用下一個/以前的鏈接,使您到該帖子中的下一個/上一個圖像,並保持圖像按正確的順序插入他們的職位。 (每個主要新聞網站似乎都有這個功能。)
目前,如果您在 WordPress 中將<?php next_image_link( false, 'Next Image' ); ?> 和<?php previous_image_link( false, 'Previous Image' ); ?> 添加到您的 image.php 或您的附件模板,如果圖像在其他地方使用,鏈接可能會將您帶到不同的帖子中的圖庫。
為了進一步説明這個問題,take a look at this gallery 。有三個圖像。如果您點擊企鵝並開始使用上一個/下一個鏈接滾動瀏覽它們,您將看到不應顯示的其他圖像!
這個問題還沒有解決。我不得不使用媒體編輯器附加帖子… 太糟糕當圖像上傳時,WP 並不總是這樣做。
最佳解決方案
這些功能將有助於:
//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="'%20.%20get_attachment_link($ppid)%20.%20'" 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="'%20.%20get_attachment_link($ppid)%20.%20'" 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;
}
通過這種方式,您可以根據需要實現上一個下一個設置,您可以在畫廊中顯示所有縮略圖。
例如。:
<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>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。