Wordpress 3.5:包含图像的自己的画廊不起作用 [英] Wordpress 3.5: own gallery with included images doesn't work
问题描述
我刚刚更新到 Wordpress 3.5,但这会导致我的代码的一小部分崩溃:有一个 php 文件,它通过 AJAX 加载带有画廊的特定帖子.
I just updated to Wordpress 3.5, but this crashed a little part of my code: There is a php file, which loads a specific post with its gallery via AJAX.
代码如下:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('../../../../wp-load.php');
$id = $_POST['id'];
// query post with this identifier
query_posts('meta_key=identifier&meta_value='.$id);
if (have_posts()) :
while (have_posts()) : the_post();
// add content
$content = apply_filters('the_content', get_the_content());
echo '<div class="content-inner">'.$content.'</div>';
endwhile;
endif;
?>
该帖子包含 [gallery] 短代码.我已经使用以下代码构建了自己的 Wordpress 画廊:
The post contains a [gallery] shortcode. I've build my own Wordpress gallery with this code:
remove_shortcode('gallery');
add_shortcode('gallery', 'parse_gallery_shortcode');
function parse_gallery_shortcode($atts) {
global $post;
extract(shortcode_atts(array(
'orderby' => 'menu_order ASC, ID ASC',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'full',
'link' => 'file'
), $atts));
$args = array(
'post_type' => 'attachment',
'post_parent' => $id,
'numberposts' => -1,
'orderby' => $orderby
);
$images = get_posts($args);
print_r($images);
}
这适用于我网站上的所有其他画廊,但不适用于 ajax 加载的画廊.它已与 Wordpress 3.4 一起使用.
This works with all other galleries on my site, but not with the ajax-loaded ones. And it has worked with Wordpress 3.4.
Wordpress 3.5 中是否有我忽略的更改?
Are there changes in Wordpress 3.5 that I've overlooked?
推荐答案
我想通了:如果您使用已上传到媒体库的图像画廊,画廊简码看起来像 [gallery ids=1,2,3]
,这意味着图片仅链接(而非附加)到图库,因此 post_type=attachment
不起作用.
I figured it out: If you use a gallery with images, which are already uploaded to the media library, the gallery shortcode looks like [gallery ids=1,2,3]
, what means, that images are only linked (and not attached) to the gallery, so post_type=attachment
doesn't work.
现在我使用正则表达式来获取图像 ID:
Now i'm using regular expressions to get the image IDs:
$post_content = $post->post_content;
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
这篇关于Wordpress 3.5:包含图像的自己的画廊不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!