0

大家好,提前感谢。我有这段代码,我将图像附加到一个帖子父级,我想将它们显示在一个带有左右箭头的滑块中。问题是,当我回显 $imagenes 时,我的代码在一个字符串中给了我 3 个 URL,我需要拆分字符串,以便我可以拥有一个独立的字符串,然后能够将其分配给 javascript 并使其工作。

我已经使用了explode、split 和str_split,但我无法得到它。我只想让变量中的每个 URL 在单击时执行操作。任何帮助将不胜感激。

这是我的代码:

<?php
        require_once("../../../../../wp-load.php");

        $id = $_GET['id'];

        $args = array('p' => $id, 'post_type' => 'myportfoliotype');

        $query = new WP_Query($args);

        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                $texto = get_the_content();
                $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID ); 
                $attachments = get_posts($args);

                if ($attachments) {
                    foreach ( $attachments as $attachment ) {
                        //echo $attachment->ID."<br>";
                        $imagenes = wp_get_attachment_image_src($attachment->ID,'fullsize',false,'');
                        $imagenes = $imagenes[0];

                    }
                }
            }
        }
        wp_reset_postdata();

        ?>
4

1 回答 1

1

改变这个:

if ($attachments) {
    foreach ( $attachments as $attachment ) {
        //echo $attachment->ID."<br>";
        $imagenes = wp_get_attachment_image_src($attachment->ID,'fullsize',false,'');
        $imagenes = $imagenes[0];
    }
}

至:

if ($attachments) {
    $imgs = array();
    foreach ( $attachments as $attachment ) {
        //echo $attachment->ID."<br>";
        $imagenes = wp_get_attachment_image_src($attachment->ID,'fullsize',false,'');
        $imgs[] = $imagenes[0];
    }
}

现在每个值$imagenes[0]都存储在自己的数组键中:

print_r($imgs);
于 2013-07-30T07:15:18.617 回答