这个previous_image_link()
仅当存在以前的图像时才打印某些内容。类似于next_image_link()
.
问题是您正在设计包含span
元素-始终存在。相反,您应该为a
标签位于span
(仅当存在下一个/上一个图像时才会显示)。
或者,挖掘源头previous_image_link
和next_image_link
呼叫adjacent_image_link
(see source). 如上所述,如果存在下一个/上一个图像,则仅打印链接。您可以创建自己的函数,该函数不打印链接,只返回true/false。However, it would still preferable to use the solution outlined above.
/**
* Similar to adjacent_image_link().
* Returns true/false if the adjacent image exists.
* @param $prev (bool) true - look for previous image, false -look for next.
*/
function wpse57904_has_adjacent_image_link($prev = true) {
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;
return isset($attachments[$k]);
}