Passing Variables via URL
-
I’m trying to write a custom image handler.
The way it would work is via ShortCode, it would grab all attached images for the current post, find the first one, post it regular size, and then thumbnails for all of the images (including the first one) underneath.
Each thumbnail would be a link to the post page with an amended “attachment=” variable in the URL. When you click the thumbnail, it should reload the same post.
Then, this variable will be used to change which attachment is displayed as the large image and allow you to “browse” through the images by reloading the page each time (see this test post for reference: https://dev.abovegroundmagazine.com/news/culture-news/art/11/02/el-mac-mural-at-montanas-paint-factory-barcelona-spain/)
Problem is, I can’t get the URL variable to pass through (should be ?attachment=$attachment->ID). All of this back-end stuff works, but:
When you click the link for the image, it throws a 404 for the page (I guess it doesn’t like the variable at the end).
I’ve tried adding some code to my Functions.php to make it work:
function add_my_query_vars($vars) { $vars[] = 'attachment'; return $vars; } add_filter('query_vars', 'add_my_query_vars');
So far, no luck.
For reference, here’s my function to generate the images:
function agm_gallery(){ echo '<div class="aligncenter">'; global $wp; global $post; if(!isset($wp->query_vars['attachment'])) { $args = array( 'post_type' => 'attachment', 'showposts' => 1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); foreach ($attachments as $attachment) { $current_attachment = $attachment->ID; } } else { $current_attachment=$wp->query_vars['attachment']; } echo wp_get_attachment_image( $current_attachment, 'full' ); $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { echo '<ul class="floating">'; foreach ( $attachments as $attachment ) { echo '<li><a href="'.add_query_arg( 'attachment', $attachment->ID, get_permalink($post->ID) ).'">'; echo wp_get_attachment_image( $attachment->ID, 'musicthumb' ); echo '</a></li>'; } echo '<div class="clearer"></div></ul></div>'; } } add_shortcode( 'agmgallery', 'agm_gallery' );
Help is greatly appreciated!
-T
- The topic ‘Passing Variables via URL’ is closed to new replies.