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.
The code looks like:
<?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;
?>
The post contains a 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);
}
This works with all other galleries on my site, but not with the ajax-loaded ones. And it has worked with WordPress 3.4.
Are there changes in WordPress 3.5 that I’ve overlooked?
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, sopost_type=attachment
doesn’t work.Now i’m using regular expressions to get the image IDs:
It’s now possible to pull all the galleries or even a single gallery using the
$post->ID
andget_post_galleries()
. Each gallery will contain the image id list inids
as well as a list of the image urls insrc
. The gallery object is basically the shortcode arguments so you have access to those as well.