Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter jlucas

    (@jlucas)

    I found a post that is similar to what I’m doing. In this the code searches a post to see if there are image attachments to that post. If there are it echos the shortcode to put the gallery in the post.

    I want to check if the post title equals the gallery title and if so it gets the gallery ID and echoes it into the code so it will post the correct gallery into each post. Hope that makes sense? Here’s the example that checks if a post has image attachments.

    /*  Function to show the post gallery just if it exists  */
    function show_gallery( $post_id ) {
    	$args = array(
    		'post_type' => 'attachment',
    		'numberposts' => null,
    		'post_status' => null,
    		'post_parent' => $post_id
    	);
    	$attachments = get_posts( $args );
    	$is_images = false;
    	// make sure the attachment(s) are image(s). otherwise, ignore them
    	foreach( $attachments as $item ) {
    		$mime_types = explode( "/", get_post_mime_type( $item->ID ) );
    		if ( in_array( 'image', $mime_types ) ) {
    			$is_images = true;
    			break;
    		}
    	}
    
    	if ( $is_images )
    		echo do_shortcode( '[gallery columns="3"]' );
    	else
    		echo '<p id="no_gallery">' . __( 'Sorry, no photos for this item.' ) . '</p>';
    }
Viewing 1 replies (of 1 total)