Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从WordPress中的帖子获取所有图库图像/附件 我需要的是: 用户通过WordPress媒体上传图库中的图片 上传并在帖子中发布。假设post_id=1,则post为: [画廊ids1,2,3,…,n] 我需要一个函数来获取此库中post_id=1的所有图像id。 图片应该是“…/post title/attachment/image title” 我试过:_Php_Wordpress_Post_Gallery_Attachment - Fatal编程技术网

Php 从WordPress中的帖子获取所有图库图像/附件 我需要的是: 用户通过WordPress媒体上传图库中的图片 上传并在帖子中发布。假设post_id=1,则post为: [画廊ids1,2,3,…,n] 我需要一个函数来获取此库中post_id=1的所有图像id。 图片应该是“…/post title/attachment/image title” 我试过:

Php 从WordPress中的帖子获取所有图库图像/附件 我需要的是: 用户通过WordPress媒体上传图库中的图片 上传并在帖子中发布。假设post_id=1,则post为: [画廊ids1,2,3,…,n] 我需要一个函数来获取此库中post_id=1的所有图像id。 图片应该是“…/post title/attachment/image title” 我试过:,php,wordpress,post,gallery,attachment,Php,Wordpress,Post,Gallery,Attachment,我的输出是: array(0) { } 我是不是太笨了?试试下面的代码 $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $page->ID); $attachments = get_posts( $args ); var_dump($attachements); 你试过使用get_post_

我的输出是:

array(0) { }

我是不是太笨了?

试试下面的代码

 $args = array( 
'post_type' => 'attachment', 
'numberposts' => -1, 
'post_status' => null, 
'post_parent' => $page->ID);

$attachments = get_posts( $args );
var_dump($attachements);

你试过使用get_post_图库吗

可用于获取文章中每个图库的信息。确保在$post_id之后传递false以仅返回数据

从那时起,您可以在不同的库中循环并从ids键中提取ID。这实际上是一个字符串,因此您需要将其添加到一个数组中,然后使用该数组添加到ID的总列表中

由于可能包含重复的id,因此运行将确保每个id只列出一次

$post_id = 298;

$image_ids = array ();

// get all the galleries in the post
if ( $galleries = get_post_galleries( $post_id, false ) ) {

    foreach ( $galleries as $gallery ) {

        // pull the ids from each gallery
        if ( ! empty ( $gallery[ 'ids' ] ) ) {

            // merge into our final list
            $image_ids = array_merge( $image_ids, explode( ',', $gallery[ 'ids' ] ) );
        }
    }
}

// make the values unique
$image_ids = array_unique( $image_ids );    

// convert the ids to urls -- $gallery[ 'src' ] already holds this info
$image_urls = array_map( "wp_get_attachment_url", $image_ids );    

// ---------------------------- //

echo '<pre>';
print_r ( $image_ids );
print_r ( $image_urls );
echo '</pre>';

输出为空:-当用户上传图像时,您需要将该图像附加到所需的帖子。这段代码适用于贴在帖子上的图片。这是贴在帖子上的图片插件非常感谢你的帮助,但这并不是我所需要的。我用自己的函数替换了WordPress[gallery]-短代码。还有一个更好的解决方案:
$post_id = 298;

$image_ids = array ();

// get all the galleries in the post
if ( $galleries = get_post_galleries( $post_id, false ) ) {

    foreach ( $galleries as $gallery ) {

        // pull the ids from each gallery
        if ( ! empty ( $gallery[ 'ids' ] ) ) {

            // merge into our final list
            $image_ids = array_merge( $image_ids, explode( ',', $gallery[ 'ids' ] ) );
        }
    }
}

// make the values unique
$image_ids = array_unique( $image_ids );    

// convert the ids to urls -- $gallery[ 'src' ] already holds this info
$image_urls = array_map( "wp_get_attachment_url", $image_ids );    

// ---------------------------- //

echo '<pre>';
print_r ( $image_ids );
print_r ( $image_urls );
echo '</pre>';