i added below code in functions.php, the test shorcode works, but gallery function doesn’t.
/*Gallery Function*/
function show_gallery_function() {
$galleryArray = get_post_gallery_ids($post->ID);
foreach ($galleryArray as $id) {
$image_alt = get_post_meta($id, '_wp_attachment_image_alt', true);
$jb_html .= "<li><a href='".wp_get_attachment_url( $id )."'>
<img width='510' src='".wp_get_attachment_url( $id )."' alt='".$image_alt."' /></a></li>";
}
return $jb_html;
}
add_shortcode('show_gallery', 'show_gallery_function');
/*Test Function*/
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
Your code looks pretty good. The problem might be that you aren’t declaring the global $post. Try using get_the_ID() instead of $post->ID.
Thanks very much. it works great.
and i test add JS code in shortcode function before foreach, it works too.
So, will don’t need to edit php files. $jb_html .= "JS code here";
function show_gallery_function() {
$galleryArray = get_post_gallery_ids(get_the_ID());
$jb_html .= "JS code here";
foreach ($galleryArray as $id) {
$image_alt = get_post_meta($id, '_wp_attachment_image_alt', true);
$jb_html .= "<li><a href='".wp_get_attachment_url( $id )."'>
<img width='510' src='".wp_get_attachment_url( $id )."' alt='".$image_alt."' /></a></li>";
}
return $jb_html;
}
add_shortcode('show_gallery', 'show_gallery_function');
nice plugin, cheers.
-
This reply was modified 9 years, 7 months ago by
Tao yunbo.
by the way, your plugin description, below –Examples–, after foreach miss a ?>
$galleryArray = get_post_gallery_ids($post->ID);
<?php foreach ($galleryArray as $id) {
<img src="<?php echo wp_get_attachment_url( $id ); ?>">
<?php } ?>
Glad it’s working, and yep, the examples are missing. Actually they probably should have PHP opening and closing around the first line also. I’ll fix that in the readme with the next update, thanks.