Hello, guys!
I trying to create image gallery in which some images will have some distinct class from another to achieve variable items width.
Can somebuddy answer to me is it possible to add such functionality to standard wp gallery? I found some solutions how to redacting gallery output by
remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'custom_gallery');
But this is not what I need.
Also I trying to add some custom checkbox to image in media library like this:
function ik_image_attachment_fields_to_edit($form_fields, $post) {
//we use this variable to set whether or not the checkbox is currently checked
//defaults to not being checked
$checked = get_post_meta($post->ID, "_ik_is_hero", true) ? "CHECKED" : "";
//to add a field to the form, there are a few values to set here
//label: the label of the field being output
//input: the type of input being output - can use 'html' as value for more custom control
//value: the current value of the input
//helps: the descriptive text shown below the field
//html: the custom html to use for a field if input is set to 'html' (see: 'ik_is_hero')
$form_fields["ik_is_hero"] = array(
"label" => __("Show In Hero Widget"),
'input' => 'html',
'html' => "<input type='checkbox'
name='attachments[{$post->ID}][ik_is_hero]'
id='attachments[{$post->ID}][ik_is_hero]'
value='1' {$checked}/><br />",
"helps" => __("If checked, this image will show in the widget."),
);
return $form_fields;
}
// now attach our function to the hook
add_filter("attachment_fields_to_edit", "ik_image_attachment_fields_to_edit", null, 2);
function ik_image_attachment_fields_to_save($post, $attachment) {
//for the standard input, we check to see if it's set and, if so, save it
//for the checkbox we save the value if it's currently checked
//if it's not checked there won't be a value set, so we set it to '0'
if( isset($attachment['ik_is_hero']) ){
update_post_meta($post['ID'], '_ik_is_hero', $attachment['ik_is_hero']);
} else {
update_post_meta($post['ID'], '_ik_is_hero', 0);
}
return $post;
}
// now attach our function to the hook
add_filter("attachment_fields_to_save", "ik_image_attachment_fields_to_save", null , 2);
This is pretty cool, but now i cant figure out how can i get this checkbox in my custom gallery. I trying to do it like this:
get_post_meta($post['ID'], '_ik_is_hero', true)
but it return to me just empty string in all cases.
This is my code for rewrite standard wp gallery:
/**
* ----------------------------------------------------------------------------------------
* 10.0 - Create custom gellery shortcode
* ----------------------------------------------------------------------------------------
*/
remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'custom_gallery');
function custom_gallery($attr, $elwidth) {
$post = get_post();
static $instance = 0;
$instance++;
# hard-coding these values so that they can't be broken
$attr['columns'] = 1;
$attr['size'] = 'full';
$attr['link'] = 'none';
$attr['orderby'] = 'post__in';
$attr['include'] = $attr['ids'];
#Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ($output != '')
return $output;
# We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if (isset( $attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if (!$attr['orderby'])
unset($attr['orderby']);
}
extract(shortcode_atts(
array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'div',
'icontag' => 'div',
'captiontag' => 'p',
'columns' => 1,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order)
$orderby = 'none';
if (!empty($include)) {
$_attachments = get_posts(
array(
'include' => $include,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
)
);
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif (!empty($exclude)) {
$attachments = get_children(
array(
'post_parent' => $id,
'exclude' => $exclude,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
)
);
} else {
$attachments = get_children(
array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
)
);
}
if (empty($attachments))
return '';
$gallery_style = $gallery_div = '';
if (apply_filters('use_default_gallery_style', true))
$gallery_style = "<!-- see gallery_shortcode() in functions.php -->";
$gallery_div = "<div class='row homepage-gallery-wrap gallery gallery-size-full'>";
$output = apply_filters('gallery_style', $gallery_style . "\n\t\t" . $gallery_div);
foreach ($attachments as $id => $attachment) {
$widthclass='';
if($elwidth == true) $widthclass = "gallery-item--width2";
$link = wp_get_attachment_link($id, 'full', false, false);
!is_page_template('template-gallery.php')? $itemclass='' : $itemclass='';
$output .= "<div class='gallery-item $widthclass $itemclass'>";
$output .= "<div class='homepage-gallery-icon'>
$link
</div>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<p class='wp-caption-text homepage-gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</p>";
}
$output .= "</div>";
}
$output .= "</div>\n";
return $output;
}
Can somebuddy answer me is it possible way to achieve such functionality, like i wrote previously? And if it right, maybe it will be obvious what i doing wrong.
Also i think about using ACF plugin for creating such gellery. Is it possible to do it with its help? Since this functionality only allowed for pro version it'l be cool to realize its possibilities before bying it.
And maybe somebuddy know some more elegant way to achieve topic target?
Thanks you a lot!