admin管理员组文章数量:1316840
I am pulling data from an external web service into a custom post type. This data includes images. How can I create an image gallery, add some existing attachments to it, and associate it with a post?
What I was hoping to find is something like a set_post_gallery
counterpart to the get_post_gallery
function, but I can't find anything like it in the codex, on google, or in wp-includes/media.php
.
This is how I set up the attachments:
$attachment = [
'guid' => wp_upload_dir()[ 'url' ] . '/' . basename( $path ),
'post_mime_type' => wp_check_filetype( basename( $path ), null )[ 'type' ],
'post_title' => "$mlsNum $id",
'post_content' => '',
'post_status' => 'inherit'
];
$attachmentId = wp_insert_attachment( $attachment, $path, $this->postId );
// Generate attachment metadata and create resized images.
wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $path ));
And this is how I am trying to retrieve the gallery for the theme:
$gallery = get_post_gallery( $post, false );
var_dump( $gallery );
var_dump( $post );
$post
is defined, and $gallery
is false. I was under the impression that wp_insert_attachment
would create a gallery for the post and add the attachment to it, but apparently this is not the case. If it was, then that would cause other problems for me when I go to attach a PDF to the post.
I am pulling data from an external web service into a custom post type. This data includes images. How can I create an image gallery, add some existing attachments to it, and associate it with a post?
What I was hoping to find is something like a set_post_gallery
counterpart to the get_post_gallery
function, but I can't find anything like it in the codex, on google, or in wp-includes/media.php
.
This is how I set up the attachments:
$attachment = [
'guid' => wp_upload_dir()[ 'url' ] . '/' . basename( $path ),
'post_mime_type' => wp_check_filetype( basename( $path ), null )[ 'type' ],
'post_title' => "$mlsNum $id",
'post_content' => '',
'post_status' => 'inherit'
];
$attachmentId = wp_insert_attachment( $attachment, $path, $this->postId );
// Generate attachment metadata and create resized images.
wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $path ));
And this is how I am trying to retrieve the gallery for the theme:
$gallery = get_post_gallery( $post, false );
var_dump( $gallery );
var_dump( $post );
$post
is defined, and $gallery
is false. I was under the impression that wp_insert_attachment
would create a gallery for the post and add the attachment to it, but apparently this is not the case. If it was, then that would cause other problems for me when I go to attach a PDF to the post.
- 1 Just had a look in the get_post_gallery function. It is regexing the post content for the gallery shortcode. That means if no shortcode present, no post gallery will be returned. – Jörn Lund Commented Mar 27, 2014 at 10:11
2 Answers
Reset to default 2When you just have raw image files, that you want to assign to a post, wp_insert_attachment
will do the job.
With attachments already present in your database you can use wp_update_post
to set the attachment's post_parent.
Like this:
wp_update_post( array(
'ID' => $attachment_id,
'post_parent' => $parent_post_id,
));
To recieve a post's attachments you can use get_children
.
$args = array(
'post_parent' => $parent_post_id,
'post_type' => 'attachment',
);
$attachments = get_children( $args );
If you insist on using get_post_gallery
– that will only return image attachments—you should add the [gallery]
shortcode to your parent post content.
There's a great PHP solution here which will add the gallery editor to the custom post type editor - great, if like me, you are creating custom post types using PHP and importing data using WPAllImport. In this case, I want to ensure I use the default gallery so I can allow my clients to edit/add/delete imported images.
The code below adds the gallery editor to your custom post type editor interface:
https://gist.github/alexdunae/897503
本文标签: plugin developmentAdd an image gallery to a custom post type
版权声明:本文标题:plugin development - Add an image gallery to a custom post type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742012940a2413232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论