Addiing captions above images in wordpress
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
edit: code was added here http://pastebin.com/gVszwf75
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 se开发者_开发百科veral blogs with just php so there has to be a way(even if I have to change my whole code).
The problem with your code is that you're looping through the array each time you call your callback function caption_image_callback()
... it has no memory of how many times it's looped!
The easiest way to fix this is to add a global variable at the beginning of the plug-in, I call it $caption_image_count
and set it equal to zero. Then call the variable in caption_image_callback()
and increment by 1 each time you call the function. This will keep track of the number of captioned images you have on the page.
If you want, you can also re-set the variable to zero before you return $post_body_content
in caption_image()
. I've posted the full solution to your pastebin: http://pastebin.com/sFe6dhqL
精彩评论