• Resolved Tao yunbo

    (@yunbotao)


    Hi Andy,

    thanks for your works on this great plugin, i made it on my site already.

    i’m thinking, if is possible to create a shortcode function, then it will be used easily.

    I was working on ToolSet plugin(types and views). it not accept php code. so i have to edit PHP files.
    if it can be wrote function in functions.php, that’s great to use shortcode in Toolset plugin.

    Thanks again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Tao yunbo

    (@yunbotao)

    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' );
    Plugin Author Andy Mercer

    (@kelderic)

    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.

    Thread Starter Tao yunbo

    (@yunbotao)

    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 8 years, 5 months ago by Tao yunbo.
    Thread Starter Tao yunbo

    (@yunbotao)

    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 } ?>
    Plugin Author Andy Mercer

    (@kelderic)

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘create shortcode function for gallery’ is closed to new replies.