Addiing captions above images in wordpress
Posted
by jacob
on Stack Overflow
See other posts from Stack Overflow
or by jacob
Published on 2010-04-24T18:06:28Z
Indexed on
2010/04/24
18:13 UTC
Read the original article
Hit count: 147
So I added some code in my css and there are boxes that appear over every image that is attached to a post. I've wanted to number the images and show the image number in the box(1...n). I have this in my functions.php
function count_images(){ global $post; $thePostID = $post->ID; $parameters = array( 'post_type' => 'attachment', 'post_parent' => $thePostID, 'post_mime_type' => 'image'); $attachments = get_children($parameters); $content = count($attachments); return $content; }
add_filter('the_content','count_images');
function caption_image_callback($matches) {
$c = count_images();
for ($i=1; $i <= $c; $i++) {
if (is_single()) {
return ''.$i.'
';
}
else {
return '';
}
}
}
function caption_image($post_body_content) {
$post_body_content = preg_replace_callback("||","caption_image_callback",$post_body_content);
return $post_body_content;
}
if ( current_user_can('edit_plugins') ) {
add_filter('the_content', 'caption_image'); }
If I run only count_images it will show the correct number of attached images to a post(let's say 15). But for some reason the number that is shown in the boxes over the images is always 1. I've seen this done on several blogs with just php so there has to be a way(even if I have to change my whole code).
PS: I had to leave spaces in some places
© Stack Overflow or respective owner