从图库短代码
1 个回答
- 投票数
-
- 2015-04-26
一些背景知识:WP将附件与帖子存储在同一数据库表中.因此,表格行对应于媒体编辑模式的字段:
get_post_gallery_images
将为您返回图像的URL,而不是数据库中的实际数据.您可以进行反向查询,然后查找包含图片URL的帖子,但这将比必要的困难.请使用get_post_gallery 函数.实际上,
从数据库中获取信息:get_post_gallery_images
也会使用此代码,但是它还会在数组中返回附件的ID.使用这些ID通过get_posts
<?php $gallery = get_post_gallery( get_the_ID(), false ); $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post__in' => explode( ',', $gallery['ids'] ) ); $attachments = get_posts( $args ); foreach ( $attachments as $attachment ) { $image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); if ( empty( $image_alt )) { $image_alt = $attachment->post_title; } if ( empty( $image_alt )) { $image_alt = $attachment->post_excerpt; } $image_title = $attachment->post_title; $image_url = wp_get_attachment_image_src( $attachment->ID, 'full' ); echo '<div class="one-third columns gallery-item">'; echo '<div class="item-picture" data-type="image">'; echo '<img src="' . $image_url[0] . '" alt="'. $image_alt .'">' ; echo '<div class="image-overlay">'; echo '<a href="' . $image_url[0] . '" data-rel="prettyPhoto[gallery]"> <span class="zoom"></span></a>'; echo '</div>'; echo '<span class="item-label">' . $image_title . '</span>'; echo '</div>'; echo '</div>'; } ?>
该脚本在
_wp_attachment_image_alt
,post_title
和post_excerpt
中查找替代标签信息,直到找到一个值.A little background: WP stores the attachments in the same database table as posts. Therefore the table rows correspond to the fields of the media edit modal:
get_post_gallery_images
will return you URLs to the images, but not the actual data in the database. You could do a reverse-query and look for posts that contain the image URL but that would be more difficult than necessary.Instead use the get_post_gallery function. This one is actually used by
get_post_gallery_images
too, but also returns the IDs of the attachments in an array. Use these IDs to get the information from the database usingget_posts
:<?php $gallery = get_post_gallery( get_the_ID(), false ); $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post__in' => explode( ',', $gallery['ids'] ) ); $attachments = get_posts( $args ); foreach ( $attachments as $attachment ) { $image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); if ( empty( $image_alt )) { $image_alt = $attachment->post_title; } if ( empty( $image_alt )) { $image_alt = $attachment->post_excerpt; } $image_title = $attachment->post_title; $image_url = wp_get_attachment_image_src( $attachment->ID, 'full' ); echo '<div class="one-third columns gallery-item">'; echo '<div class="item-picture" data-type="image">'; echo '<img src="' . $image_url[0] . '" alt="'. $image_alt .'">' ; echo '<div class="image-overlay">'; echo '<a href="' . $image_url[0] . '" data-rel="prettyPhoto[gallery]"> <span class="zoom"></span></a>'; echo '</div>'; echo '<span class="item-label">' . $image_title . '</span>'; echo '</div>'; echo '</div>'; } ?>
The script looks for alt-tag info in
_wp_attachment_image_alt
,post_title
andpost_excerpt
until it finds a value.-
您好Jan非常感谢您的帮助和补充.仍然无法获取图库图像/后元数据,该页面未返回任何结果.image_url如何获取图库图片的网址?我尝试将`$image_url=post_guid;`设置为失败.explode函数使用字符串(图像ID)并将其转换为get_posts和get_post_meta使用的数组,是吗?我也在一些方面引用了该编解码器:[https://codex.wordpress.org/Function_Reference/get_post_gallery]和[https://codex.wordpress.org/Function_Reference/wp_get_attachment_image]Hi Jan thanks so much for your help and augmenting details. Still having trouble getting the gallery images/post-meta, the page returns no results. How does image_url obtain the url of the gallery image? I tried setting `$image_url = post_guid;` to no success. The explode function is taking a string (image IDs) and turning this into an array used by get_posts and get_post_meta, yes? I'm reference the codex too for some clues: [https://codex.wordpress.org/Function_Reference/get_post_gallery] and [https://codex.wordpress.org/Function_Reference/wp_get_attachment_image]
- 0
- 2015-04-26
- ccbar
-
我疯了,实际上忘记了包含image_url.抱歉我已经更新了答案.请注意,`wp_get_attachment_image_src`返回一个数组,其中包含url,宽度,高度和"boolean:如果$ url是调整大小的图像,则为true;如果原始图像或没有可用图像,则为false.I'm a nut and actually forgot to include image_url. Sorry for that. I have updated the answer. Note that `wp_get_attachment_image_src` returns an array that contains the url, width, height and "boolean: true if $url is a resized image, false if it is the original or if no image is available."
- 0
- 2015-04-27
- Jan Beck
-
Jan,您好,页面结果仍为空.我还尝试将新图像添加到新库中,以查看是否需要上传新图像,而不是媒体库中的现有图像,以便查看是否可以获得任何结果,但是没有.因此仍在学习php,但是`$image_url [0]`意味着设置为null直到传递ID为止?唯一可靠的方法是获取画廊的短代码,就像我的第一个问题一样,只是无法获得该死的标题.Hi Jan, still empty page results. I also tried adding new images to a new gallery to see if I needed to upload new images, not existing images within media library, in order to see if I could get any results, but no. So still learning php but `$image_url[0]` means to set to null until the ID is passed? the only thing that's worked reliably is getting the shortcode for gallery as in my first question, just can't get the darn title.
- 0
- 2015-04-27
- ccbar
-
那么计数是否必须从0开始,我可以有一个空的括号[],但仍然可以通过它传递图像ID吗?`$image_url [0]`与`$image_url []`So does the counting have to start from 0, can I have an empty bracket[] and still have image IDs passed though it? `$image_url[0]` vs `$image_url[]`
- 0
- 2015-04-27
- ccbar
-
稍后再尝试,同时查看文档https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_srcI'll try it later, meanwhile have a look at the documentation https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
- 0
- 2015-04-27
- Jan Beck
-
了不起!谢谢,我终于明白了.[0]指定数组的第一个索引ID.私下里,我可以给您发送指向测试页的链接,如果有任何不同的话.Terrific! thank you, I finally get it. [0] specifics the first index of the array, the ID. privately I could send you a link to the test page, if that makes any difference.
- 0
- 2015-04-28
- ccbar
-
很高兴你想出来了.[0]实际上是包含图像直接URL的数组的第一个索引.我在开发环境中运行了代码,并注意到get_post_gallery需要将第二个参数设置为`false`,否则它将不返回数组,而是画廊的html.我编辑了原始答案以反映这一点.Glad you figured it out. [0] is indeed the first index of the array that contains the direct URL to the image. I ran the code in my dev environment and notices that get_post_gallery needs a second parameter set to `false` otherwise it will not return an array but html of the gallery. I edited the original answer to reflect that.
- 0
- 2015-04-28
- Jan Beck
-
好极了!有用!谢谢.-由于某些原因,以上代码尚未编辑.但是,一旦确定,我将检查答案是否正确.Yay! it works! Thank you. -- the code above isn't yet edited, for some reason. But as soon as it is, I'll check the answer as correct.
- 0
- 2015-04-28
- ccbar
-
现在应该更改.很高兴可以为您服务.should be changed now. Glad I could help you out.
- 0
- 2015-04-29
- Jan Beck
-
不胜感激!我可以分享吗?Greatly appreciated! May I share it?
- 0
- 2015-04-29
- ccbar
-
分享什么?我给的答案?Share what? The answer I gave?
- 0
- 2015-04-29
- Jan Beck
-
随意做任何你想做的事情:)Feel free to do whatever you want with it :)
- 0
- 2015-05-05
- Jan Beck
-
是的,您的回答非常重要而且很有帮助.谢谢.Yes, you answer, so much needed and helpful. Thank you.
- 0
- 2015-05-21
- ccbar
我正在尝试学习如何编写php,以便可以在WordPress中自定义图片库(以及其他自定义项).
我拥有的代码非常适合样式化的图库页面,但是很难弄清楚如何在WP画廊中获取图像的标题和alt属性(我想这是因为这些图片位于图库功能中,因此它们不被视为帖子的附件).
我想使用WP画廊,因为我想将WP内置功能用于画廊.
感谢您的帮助...这是最愚蠢的事情吗?
注意:在尝试编辑页面上的图像时,如果不使用WP Gallery中的旧附件或已从页面中删除的孩子仍然显示,则get_attachment或get_children也是灾难性的)