我需要获取与中的帖子相关联的所有图库图像的URL(实际大小)single.php
. 以下代码来自Codex 返回大小非常小的150x150的所有图像,这不适合我想要的
<?php
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
$gallery = get_post_gallery( get_the_ID(), false );
foreach( $gallery[\'src\'] AS $src )
{
?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
}
endif;
endwhile;
?>
然后,我找到了以下按实际大小重新运行图像的方法
BUT 返回图库中的所有图像(不仅是当前帖子的图库中的图像)
<?php
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array(\'post_parent\' => $post_id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => \'ASC\', \'orderby\' => \'menu_order ID\') );
print_r( $images );
if ($images) :
foreach ($images as $attachment_id => $image) :
if ( $image->ID != $thumbnail_ID ) :
$img_alt = get_post_meta($attachment_id, \'_wp_attachment_image_alt\', true); //alt
if ($img_alt == \'\') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, \'large\' );
$img_url = $big_array[0];
echo \'<li>\';
echo \'<img src="\';
echo $img_url;
echo \'" alt="\';
echo $img_alt;
echo \'" />\';
echo \'</li><!--end slide-->\';
endif; endforeach; endif;
所以我试图通过添加
$post_id = get_the_ID();
让与:
<?php
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$post_id = get_the_ID();
$images = get_children( array(\'post_parent\' => $post_id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => \'ASC\', \'orderby\' => \'menu_order ID\') );
print_r( $images );
if ($images) :
foreach ($images as $attachment_id => $image) :
if ( $image->ID != $thumbnail_ID ) :
$img_alt = get_post_meta($attachment_id, \'_wp_attachment_image_alt\', true); //alt
if ($img_alt == \'\') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, \'large\' );
$img_url = $big_array[0];
echo \'<li>\';
echo \'<img src="\';
echo $img_url;
echo \'" alt="\';
echo $img_alt;
echo \'" />\';
echo \'</li><!--end slide-->\';
endif; endforeach; endif;
但是现在我只得到了一张白页,而没有任何白页!你能告诉我为什么会发生这种情况以及我如何解决这个问题吗?坦斯克
最合适的回答,由SO网友:birgire 整理而成
正如我在其他地方解释的,您可以修改get_post_gallery()
或get_post_gallery_images()
使用简单过滤器:
// Add a filter for full gallery size:
add_filter( \'shortcode_atts_gallery\',\'wpse_full_size_gallery\' );
$gallery = get_post_gallery( get_the_ID(), false );
// Remove the filter:
remove_filter( \'shortcode_atts_gallery\',\'wpse_full_size_gallery\' );
其中:
function wpse_full_size_gallery( $out )
{
$out[\'size\'] = \'full\'; // Edit this to your needs! (thumbnail, medium, large, ...)
return $out;
}
这样可以避免运行其他
wp_get_attachment_image_src
调用以从附件ID检索全尺寸图像。
因此,您的第一个代码示例是:
<?php
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
// Add a filter for full gallery size:
add_filter( \'shortcode_atts_gallery\',\'wpse_full_size_gallery\' );
$gallery = get_post_gallery( get_the_ID(), false );
// Remove the filter:
remove_filter( \'shortcode_atts_gallery\',\'wpse_full_size_gallery\' );
foreach( $gallery[\'src\'] AS $src )
{
?>
<img src="<?php echo $src; ?>"
class="my-custom-class"
alt="Gallery image" />
<?php
}
endif;
endwhile;
?>
以全尺寸显示库图像。
请注意get_children()
函数正在获取上载到给定帖子的所有附件。因此,在大多数情况下,它不会给出与gallery短代码相同的结果。