Wordpress PHP获取图片库的URL?我有一部分代码在工作


Wordpress PHP get image gallerys URL? I got part of the code working

你好,我正试图在我的WordPress中创建灯箱库,我设法使大部分代码都能很好地从库中抓取图像。然而,我不知道如何获得他们原始文件的URL。。。。

PHP

<?php $post_content = get_the_content();
    preg_match('/'[gallery.*ids=.(.*).']/', $post_content, $ids);
    $array_id = explode(",", $ids[1]);
    $item = 0;
    while ($array_id[$item] != ''){
        if($item == 0){
            echo wp_get_attachment_image($array_id[$item], 'medium');
        }else{
                    echo "<a ref='".wp_get_attachment_image($array_id[$item])."'>";}                        
        $item++;
        }
?>

所以我的想法是在画廊中显示我用第一个ECHO完成的第一个图像的IMG,而不是所有其他图像,我只想要原始文件的链接。

我的最终代码需要看起来像这样:

<a rel="group1" href="image_big_1.jpg"><img src="image_small_1.jpg" alt=""/></a>
<a rel="group1" href="image_big_3.jpg"></a>
<a rel="group1" href="image_big_4.jpg"></a>
<a rel="group1" href="image_big_5.jpg"></a>
<a rel="group1" href="image_big_6.jpg"></a>

另外,你对rel=''有什么建议?我需要这个对每个有画廊的帖子都是唯一的。我不确定该用什么。我不能用TITLE,因为如果他们进入多个标题相同的画廊,就会出现问题。我猜可能是current_time定义的某种(RANDOM)前缀?

请帮忙感谢

我找到了一个解决方案,并编辑了我的代码以工作:

这是实现上述目标的代码:

<?php $post_content = get_the_content();
    preg_match('/'[gallery.*ids=.(.*).']/', $post_content, $ids);
    $array_id = explode(",", $ids[1]);
    $item = 0;
    while ($array_id[$item] != ''){
        $url = wp_get_attachment_image_src($array_id[$item]);
            if($item == 0){
                echo "<a href='".$url[0]."'>".wp_get_attachment_image($array_id[$item], 'gallery-thumb')."</a>";
            }else{
                echo "<a href='".$url[0]."'</a>";}                      
        $item++;
        }
?>

使用的参考:http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

谢谢大家!