lundi 23 mars 2015

How to parse a post and change the way a gallery item is rendered?


I want to use this plugin http://ift.tt/1CewEC0 to display the gallery the users insert in their posts, for that I need to use an specific HTML structure so the plugin works, I've been looking everywhere and tried what I found but with no success, the closer I found whas this post http://ift.tt/19dU7HJ, and more precisely, one of the comments that shows how to render the galleries in the place the user inserted them, instead at the beggining as the posts intends to, which is this code:



remove_shortcode('gallery');
add_shortcode('gallery', 'parse_gallery_shortcode');

function gallery_output_in_the_content($content) {
$g = parse_gallery_shortcode();
$custom_content = $g;
$custom_content .= $content;
return $custom_content;
}

add_filter('the_content', 'gallery_output_in_the_content');

function parse_gallery_shortcode($atts) {
//… as above but with this extra code:

$gal .= '';

foreach ($images as $image) {
$caption = $image->post_excerpt;

$description = $image->post_content;
if($description == '') $description = $image->post_title;

$image_alt = get_post_meta($image->ID,'_wp_attachment_image_alt', true);

// render your gallery here
$gal .= '';
$img_meta = wp_get_attachment_metadata($image->ID);
$img = wp_get_attachment_image_src($image->ID, 'gallery-small');
$img_big = wp_get_attachment_image_src($image->ID, 'gallery-big');
$gal .= '';
if (strlen($caption) > 0) $gal .= '' . $caption . '';
$gal .= '';
}

$gal .= '';

return $gal;
}


I'm putting this code in functions.php of my theme and rendering simply the_content in my single.php


But I'm getting an error, the variable $atts is not defined, and indeed it is not, but I do not understand where should I get the value for it, if at all.


Following that variable I found that wp-includes/media.php is the file that renders the gallery item, but I do not want to change it there as I'm working on a team in this projecy and this file is not included in the git repository.


Any idead for fixing the error or more importantly, to parse the gallery item in the post so it rendrs the way I want to?





Aucun commentaire:

Enregistrer un commentaire