WordPress Gallery on single page - php

My current WordPress theme has a built in gallery. Each gallery displays the first / main thumbnail and once clicked opens the entire content of this specific gallery in a shadowbox. My ideal goal now is to find a way to alter this that instead of displaying the shadowbox directly it would direct to a single page with the specific galleries content.
As below my current gallery code:
<div id="maincontentwrap">
<div class="main-sep">
</div><!-- .main-sep -->
<div id="contentwrap">
<div id="content-gallery" role="main">
<?php
$wp_query = new WP_Query();
$wp_query->query( array( 'post_type' => 'galleries', 'posts_per_page' => of_get_option('le_gallery_items'), 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$mod = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
if(has_post_thumbnail()) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$src = $src[0];
}
else{
foreach ( $attachments as $id => $attachment ) {
$src = wp_get_attachment_url( $id );
}
}
if ($mod % 3 == 0) {
echo ' <div class="gallery-entry-img-last">';
}
else{
echo ' <div class="gallery-entry-img">';
}
?>
<div class="gallery-entry-img-l"><a rel="next" href="<?php echo $src; ?>"><span class="rollover" ></span><img class="blog-entry-img" src="<?php echo get_bloginfo('stylesheet_directory'); ?>/library/tools/timthumb.php?src=<?php echo $src; ?>&w=270&h=198" alt="<?php get_the_title(); ?>"/></a>
<span class="gallery-entry-img-tab"></span>
</div>
<span class="gallery-index-title"><?php the_title(); ?></span>
<div class="hidden-gallery">
<?php
$pic_count = 0;
foreach ( $attachments as $id => $attachment ) {
$pic_count++;
$others_src = wp_get_attachment_url( $id );
?>
<a rel="next" href="<?php echo $others_src; ?>" title="<?php the_title(); ?>"></a>
<?php
}
?>
</div><!-- .hidden-gallery-->
</div>
<?php
$mod++;
endwhile;
?>
<div style="clear:both;"></div>
</div>
</div><!-- #contentwrap-->
</div><!-- #maincontentwrap-->
Would anybody have an idea how I can have this achieved? Some expert advise would be truly appreciated.

Use wp_get_attachment_link( $id, $size, $permalink, $icon, $text ); (see http://codex.wordpress.org/Function_Reference/wp_get_attachment_link). This outputs the link to the attachment page. So you just need to adapt your code with this function. Use value = 0 for the $permalink argument so your link brings you to the attachment page (value = 1 will bring you to the file itself).
Beware, though, that the "shadowbox" is probably triggered by event binding (clicking on the anchor) in a JavaScript, so you might have to adapt your HTML code for the links. If the selector in the script for shadowbox uses a class name or an ID to detect the click, you will probably need to change the ID or class name of the container of your link in order to be sure that your target page does not get displayed within the shadowbox.
You can also (and should, actually) use wp_deregister_script() to deregister the script that displays the shadowbox, considering you don't need it in this page.

Related

Use an image as thumbnail instead of slider(wordpress)

Hi I am making an wordpress website where I am going to upload amount of pictures.
Problem is if I upload more than 1 image on the same post or page, wordpress makes a slideshow automatically 'and makes a slider thumbnail also'.
I don't want any slidershows on the post or page and thumbnail also shouldn't slide.
this is my slider code:
function rowling_flexslider($size) {
if ( is_page()) :
$attachment_parent = $post->ID;
else :
$attachment_parent = get_the_ID();
endif;
if($images = get_posts(array(
'post_parent' => $attachment_parent,
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => null,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
))) { ?>
<?php if ( !is_single() ) : // Make it a link if it's an archive ?>
<a class="flexslider" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php else : // ...and just a div if it's a single post ?>
<div class="flexslider">
<?php endif; ?>
<ul class="slides">
<?php foreach($images as $image) {
$attimg = wp_get_attachment_image($image->ID,$size); ?>
<li>
<?php echo $attimg; ?>
<?php if ( !empty($image->post_excerpt) && is_single()) : ?>
<p class="post-image-caption"><span class="fa fw fa-camera"></span><?php echo $image->post_excerpt; ?></p>
<?php endif; ?>
</li>
<?php }; ?>
</ul>
<?php
if ( !is_single() )
echo '</a>';
else
echo '</div>';
}
}
How should I do? I tried to delete php codes but it wasn't a good way.

how to display all attached thumbnails of a page in the main slider

i want to display all the attached images of a page in the main slider of my home page. i have been using following code..
<div class="carousel-inner">
<?php if(have_posts()) : while (have_posts()) : the_post();?>
<?php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {?>
<div class="item active">
<img src="<?php echo wp_get_attachment_src( $attachment_id, 'full' ); ?>" alt="First slide">
</div>
<?php
}
}
?>
<?php endwhile;else: ?>
<?php echo "No slider Images found" ?>
<?php endif; ?>
</div>
But its not loading the images . what is it that i am doing wrong . please help me out
This is how im doing it
$args = array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
);
$attachments = get_posts($args);
foreach ($attachments as $att)
{
$imgThumb = image_downsize($att->ID, 'slider-image');
echo $img = '<img src="'.$imgThumb[0].'" alt="'.$att->postitle.'" />';
}
This is part of the code (its more complicated at its later put the image in <li> elements and sorts them and so on - but this should tell you how you may get images.
image_downsize is wordpress command that takes attachment id and its size (named or array of width and height) and then it returns the path to the image with that size (it also scales the image if needed).
You are using wp_get_attachment_image_src() function wrong way.
Here is the corrected code
<div class="carousel-inner">
<?php if(have_posts()) : while (have_posts()) : the_post();?>
<?php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full'); // returns an array
?>
<div class="item active">
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
</div>
<?php
}
}
?>
<?php endwhile;else: ?>
<?php echo "No slider Images found" ?>
<?php endif; ?>
</div>

WordPress: Not able to get current ID.

I'm very new to WordPress, and I've been pulling hair trying to figure this one out.
I have a custom theme, the theme is very simple, which contains an image carousel built up from a collection of images in a gallery.
If I remove the carousel include file, the post ID on the rest of the page returns correctly, as soon as I add the carousel back, it uses the ID of the last category pulled in the code.
I removed all 'global' references, as I assumed this will override the ID for the rest of the page, but it's still wrong.
The code from the carousel.php file:
<div id="carousel">
<?php
$args = array(
'post_type' => 'gallery',
'post_status' => 'publish',
'name' => $wp_query->query_vars['name'],
'posts_per_page' => 1
);
$second_query = new WP_Query($args);
$gllr_options = get_option('gllr_options');
$gllr_download_link_title = addslashes(__('Download high resolution image', 'gallery'));
if ($second_query->have_posts()) : while ($second_query->have_posts()) : $second_query->the_post(); ?>
<div class="carousel-holder">
<?php
the_content();
$galleries = get_posts(array(
'showposts' => -1,
'what_to_show' => 'posts',
'post_status' => 'inherit',
'post_type' => 'attachment',
'orderby' => $gllr_options['order_by'],
'order' => $gllr_options['order'],
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',
'post_parent' => $post->ID
));
if (count($galleries) > 0) { ?>
<ul id="carousel-gallery">
<?php foreach ($galleries as $attachment) {
$key = 'gllr_image_text';
$link_key = 'gllr_link_url';
$alt_tag_key = 'gllr_image_alt_tag';
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'photo-thumb' );
$image_attributes_large = wp_get_attachment_image_src( $attachment->ID, 'large' );
$image_attributes_full = wp_get_attachment_image_src( $attachment->ID, 'full' );
if ( 1 == $gllr_options['border_images'] ) {
$gllr_border = 'border-width: ' . $gllr_options['border_images_width'] . 'px; border-color:' . $gllr_options['border_images_color'] . '';
$gllr_border_images = $gllr_options['border_images_width'] * 2;
} else {
$gllr_border = '';
$gllr_border_images = 0;
}
if (($url_for_link = get_post_meta($attachment->ID, $link_key, true)) != "") { ?>
<li>
<img alt="<?php echo get_post_meta($attachment->ID, $alt_tag_key, true); ?>" title="<?php echo get_post_meta( $attachment->ID, $key, true ); ?>" src="<?php echo $url_for_link; ?>" />
</li>
<?php } else { ?>
<li rel="gallery_fancybox<?php if ( 0 == $gllr_options['single_lightbox_for_multiple_galleries'] ) echo '_' . $post->ID; ?>">
<img alt="<?php echo get_post_meta($attachment->ID, $alt_tag_key, true); ?>" title="<?php echo get_post_meta( $attachment->ID, $key, true ); ?>" src="<?php echo $image_attributes_large[0]; ?>" />
</li>
<?php }
$count_image_block++;
} ?>
</ul>
<div class="clearfix"></div>
<div id="arrows" class="arrows">
<a id="prev" class="prev" href="#"></a>
<a id="next" class="next" href="#"></a>
</div>
<div id="paginator" class="paginator"></div>
<?php } ?>
</div>
<?php endwhile; else: endif;?>
</div>
The code that is returning the erroneous ID:
<div class="left sidebar">
<?php echo the_ID(); ?>
</div>
This is within a template page, that looks like this:
<?php
/**
* Template Name: 2 Column Left
*/
get_header();
?>
<div id="maincontent">
<div id="content" class="site-content" role="main">
<div class="left sidebar">
<?php
echo the_ID();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
Carousel.php is defined within header.php, and then gets called when get_header(); is called.
So, assuming I'm on the About page, with an ID of 16, and the carousel calls a gallery with page ID of 8, the echo in left-sidebar always returns 8.
I'm at a loss here, I've search high and low for an answer to this problem, but I've come up with nothing.
Try resetting the loop using the wp_reset_query() method.
Place this right after your carousel loop ends.
<?php wp_reset_query(); ?>

Getting image attachments in FlexSlider for Wordpress

I'm trying create a function that will load image attachments of a post into a list for FlexSlider to use. To specify, I want an image slider that is specific to a certain post. There may be many sliders on one page depending on the post type (in this case, the image slider type) I want.
I've run into an issue, however. This is in my functions.php file, a variation of Marty Spellerberg's jQuery Slideshow article (I would post a link but I can't post more than 2 links with less than 10 rep):
function flexslider($post_id) {
global $post;
$images = get_children(array('post_parent' => $post_id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_title = $image->post_title;
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ($img_alt == '') : $img_alt = $img_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
?>
<li><img src="<?php echo $img_url; ?>" alt="<?php echo $img_alt; ?>" title="<?php echo $img_title; ?>" /></li>
<?php endforeach; ?>
<?php endif;
}
And this is what's calling the function in index.php:
<div class="flexslider">
<ul class="slides">
<?php flexslider('large','$post->ID'); ?>
</ul>
</div>
The problem is, it's only showing up on the permalink page. I want it to show up wherever the post is shown.
An example can be seen here: permalink and homepage.
Edit: Updated double quotes to singular quotations as per suggestion from comment.
Here is my solution. I basically did something a bit similar, but compressed it into one function. You can find more details on my blog.
<?php
function add_flexslider() { // display attachment images as a flexslider gallery
$attachments = get_children(array('post_parent' => get_the_ID(), 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'attachment', 'post_mime_type' => 'image','caption' => $attachment->post_excerpt, ));
if ($attachments) { // see if there are images attached to posting ?>
<!-- Begin Slider -->
<div class="flexslider">
<ul class="slides">
<?php // create the list items for images with captions
foreach ( $attachments as $attachment_id => $attachment ) {
echo '<li>';
echo wp_get_attachment_image($attachment_id, 'large');
echo '<p>';
echo get_post_field('post_excerpt', $attachment->ID);
echo '</p>';
echo '</li>';
} ?>
</ul>
</div>
<!-- End Slider -->
<?php } // end see if images
} // end add flexslider
?>

Wordpress WP_Query

For some reason I cannot get attachment to display if i pass in $attachment_id if i pass a real value in like 187 it works.
I am using WpAlchemy and Custom Image Sizes plugin.
<section id="new">
<?php $myquery = new WP_Query(array('post_type' => array('post', 'website_gallery'),'showposts' => '2'));
while ($myquery->have_posts()) : $myquery->the_post();
global $custom_metabox;
?>
<div class="latest hentry">
<h2><?php the_title(); ?></h2>
<?php $website_gallery->the_meta(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php $website_gallery->the_value('galleryimage');?>" alt="<?php the_title(); ?>">
</a>
<?php echo wp_get_attachment_image($attachment_id, '220x80'); ?>
</div>
<?php endwhile; wp_reset_query(); ?>
</section>
I think you can get the attachment id by using the get_post() function. get_post() requires an array for it's parameter and that array can be something like this:
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
Since you want attachments, make sure your 'post_type' => 'attachment'.
You can then do:
$attachments = get_post( $args );
if( $attachments ) {
foreach( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, '220x80' );
}
}
Adapt that code for what you need. Hopefully it'll work for you.
Check out: http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

Categories