需要从阵列获取特定数据

时间:2017-01-09 作者:warm__tape

我正在为一位客户建造一个定制旋转木马。我有一个函数,可以从贴在帖子上的所有图像中获取三幅图像的块:

global $rental;

$images = get_children( array(
    \'post_parent\' => $rental_id,
    \'post_status\' => \'inherit\',
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image\',
    \'order\' => \'ASC\',
    \'orderby\' => \'menu_order ID\'
) );

$array = $images;
$number_of_elements = 3;
$count = count( $array );
$split = array();

for ( $i = 0; $i <= $count - 1; $i++ ) {
    $slices = array_slice( $array, $i , $number_of_elements);
    if ( count( $slices ) != $number_of_elements )
        break;

    $split[] = $slices;
}

if ($split) :
    foreach ($split as $outeritem) :    
        echo \'<div class="Outer Top">\';
            foreach ($split as $inneritem) :
                echo \'<div class="Inner Top">\';
                echo \'<img src="\' . $inneritem . \'">\';
                echo \'</div>\';
             endforeach;
        echo \'</div>\';
    endforeach;
endif;

//print_r( $split );
我要做的就是更换inneritem 使用图像的URL。数据都在一个数组中,正如您所看到的,我只需要为每个项目提取guid的值。下面的数组来自取消注释print_r( $split ); 为了保持整洁,我删除了所有无关数据:

Array (
    [0] => Array (
        [0] => WP_Post Object (
            [ID] => 120
            [guid] => http://******/wp-content/uploads/2016/12/T15923-11-1-1.jpg
        )
        [1] => WP_Post Object (
            [ID] => 121
            [guid] => http://******/wp-content/uploads/2016/12/T15923-12-1-1.jpg
        )
        [2] => WP_Post Object (
            [ID] => 122
            [guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
        )
    )
    [1] => Array (
        [0] => WP_Post Object (
            [ID] => 121
            [guid] => http://******/wp-content/uploads/2016/12/T15923-12-1-1.jpg
        )
        [1] => WP_Post Object (
            [ID] => 122
            [guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
        )
        [2] => WP_Post Object (
            [ID] => 123
            [guid] => http://******/wp-content/uploads/2016/12/T15923-13-1-1.jpg
        )
    )
    [2] => Array (
        [0] => WP_Post Object (
            [ID] => 122
            [guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
        )
        [1] => WP_Post Object (
            [ID] => 123
            [guid] => http://******/wp-content/uploads/2016/12/T15923-13-1-1.jpg
        )
        [2] => WP_Post Object (
            [ID] => 124
            [guid] => http://******/wp-content/uploads/2016/12/T15923-14-1.jpg
        )
    )
)

2 个回复
最合适的回答,由SO网友:Tunji 整理而成

你应该能够重写你所拥有的,并使用get_permalink@Benoti 在省略$split 大堆

get_permalink 接受Post ID或Post对象。

global $rental;

$images = get_children( array(
    \'post_parent\' => $rental_id,
    \'post_status\' => \'inherit\',
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image\',
    \'order\' => \'ASC\',
    \'orderby\' => \'menu_order ID\'
) );

$array = $images;
$number_of_elements = 3;
$count = count( $array );

for ( $i = 0; $i <= $count - 1; $i++ ) {
    $slices = array_slice( $array, $i , $number_of_elements);
    if ( count( $slices ) != $number_of_elements )
        break;

    echo "<div class=\'Outer Top\'>";
    foreach( $slices as $inneritem ) {
        $link = wp_get_attachment_url( $inneritem->ID );
        echo "<div class=\'Inner Top\'>";
        echo "<img src=\' $link  \'>";
        echo "</div>";
    }
    echo "</div>";
}

SO网友:Benoti

我没有测试任何东西,也没有阅读您的代码,但您似乎可以获得\\u permalink(),正如我在评论中所说的那样,但您确实可以获得附件页,而不是其url。

您可以轻松访问对象ID、guid

wp_get_attachment_url($inneritem[$i]->ID);
所以

if ($split) :
foreach ($split as $outeritem) :    
    echo \'<div class="Outer Top">\';
    $i=0;
        foreach ($split as $inneritem) :
            echo \'<div class="Inner Top">\';
            echo \'<img src="\' . wp_get_attachment_url($inneritem[$i]->ID) . \'">\';
            echo \'</div>\';
            $i++;
         endforeach;
    echo \'</div>\';
    endforeach;
endif;

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请