Reputation: 4222
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 [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);
}
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?
Upvotes: 3
Views: 3956
Reputation: 1309
It's now possible to pull all the galleries or even a single gallery using the $post->ID
and get_post_galleries()
. Each gallery will contain the image id list in ids
as well as a list of the image urls in src
. The gallery object is basically the shortcode arguments so you have access to those as well.
if ( $galleries = get_post_galleries( $post->ID, false ) ) {
$defaults = array (
'orderby' => 'menu_order ASC, ID ASC',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'full',
'link' => 'file',
'ids' => "",
'src' => array (),
);
foreach ( $galleries as $gallery ) {
// defaults
$args = wp_parse_args( $gallery, $defaults );
// image ids
$args[ 'ids' ] = explode( ',', $args[ 'ids' ] );
// image urls
$images = $args[ 'src' ];
}
}
Upvotes: 1
Reputation: 4222
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.
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]);
Upvotes: 2