• [“Urgent, please help.” removed from topic – you need to hire someone if you need urgent help – these are volunteer forums ]

    Hello all,

    I need a way to do this asap. I have about 10 different folders of images. In each folder is a file called “labels.txt” This text file has 1 photo filename and its corresponding caption, separated by a divider, per line, like so:

    photo-001.jpg|caption
    photo-002.jpg|caption
    photo-003.jpg|caption
    photo-004.jpg|caption

    The issue is, each folder has hundreds of photos. Every single WP gallery plugin I’ve come across does not seem to have an option to pull the captions from a text file, spreadsheet, or anything like it. It seems as though captions have to be added one by one to each folder, which would take me so much time that I might literally go insane from the monotony.

    Can anyone please offer me any help with how to have galleries on my WP site where the photos display as a thumbnail grid, and once a photo is clicked, opens in a lightbox with the appropriate caption? This is the only thing keeping this site from being completed and I’ve been tearing my hair out for weeks looking for a solution.

    Thank you so much in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • wpismypuppet

    (@wordpressismypuppet)

    So you are looking to keep the folder structure, with the images inside those folders, with the text file that contains the captions for each image? You don’t want to upload those images to WordPress and place the caption on the image there?

    There are a lot of solutions (relatively easy ones) that use PHP’s glob function and file_get_contents() function to do what you are asking for.

    Right now, for a quick fix, it may seem like a good option. But in the long run, if you are going to be adding more images and captions down the road, WordPress is a much better solution. Trust me as I’ve done both options extensively. Almost every PHP folder/text file solution I’ve created for a client (created for the same reason you have mentioned… monotonous work) has been converted over to WordPress due to the ease of use (and lack of photo manipulation/programming/FTP experience).

    The reason no WP plugin exists that pull from a text file is because it’s not necessary… and in fact it’s repetitive and pointless based on the way WordPress works. But since you already have a lot of work started, I can see how you are looking to avoid duplicating work just to get the site done.

    Let me know which path you want to travel down and I can offer some assistance.

    Thread Starter 39images

    (@39images)

    Hi there,

    Thank you so much for your reply!

    We realistically won’t be adding any more photos. I would add the captions in WP, but some of these galleries have over 1,000 photos in them and I shudder at the thought of having to add those captions manually. I’m using NextGEN Gallery, but that doesn’t allow any caption importing. It can import metadata, assuming the captions are already attached to their respective photos. In my case, they aren’t. If you have a quick and easy solution I would REALLY appreciate it because my back is against the wall here!

    wpismypuppet

    (@wordpressismypuppet)

    Well, I guess in your case you will want to use the full PHP approach. Forget all that is WordPress for this gallery… no plugin (that I know of) will allow you to do both. How good are you at PHP?

    I would still use glob() to get all the images inside the folders, but I changed my mind and would instead use fopen() and fgets(). fopen() simply opens the text file and fgets() will get one line at a time and place that into a string. Since each of your images|caption are on separate lines, this makes the most sense.

    Make two template files… one to hold ALL galleries showing a single thumbnail for each gallery and making it clickable to take you to that particular gallery. Then make another template file that will show all the images inside that particular gallery. BOTH templates will work almost exactly the same… the difference being in the text files read and the path to start from.

    Set up your folder structures
    Start with a galleries folder inside your images folder (images/galleries). This will be the “root” where all the galleries live. In fact, if you do the code right, all you’ll have to do is create a new folder inside the galleries folder, populate it with images and a text file, and the code will dynamically add that gallery to your site automatically.

    Inside this folder should now be all your galleries in separate folders. i.e. “images/galleries/gallery-1”, “images/galleries/gallery-2” and so forth. Name them whatever you like though, it doesn’t matter. It doesn’t even have to follow numeric importance or anything, unless you want to “order” your gallery a certain way.

    Inside each of these gallery folders should be a folder called “thumbnails”… i.e. “images/galleries/gallery-1/thumbnails”. This will, of course, hold all the thumbnails for each larger version image. You can skip this step if you plan on using timthumb to generate the thumbnails on the fly.

    Inside each of these thumbnails folder, create a “featured.jpg” thumbnail that will mimic your WordPress featured image. This way you’ll always know to call featured.jpg whenever you want to show the featured image for any gallery. We’ll get to this in the next section.

    Code the Landing Page (the one that show all the galleries you have)
    At this point, code the first template. Use glob() to gather all of the folders inside your root “images/galleries”. Make sure you use if statements to remove unnecessary folders such as ‘.’ and ‘..’. Then loop through each result calling for the featured.jpg image inside the folder’s “thumbnails” folder and add a hyperlink around the image to take the user to your second template, passing the folder name in the URL. This is how the second template will know which folder to show.

    If you want to get fancy, you can create a text document in the root that has description information for each gallery… the way to read this file is exactly the same way as we’ll use for the second template.

    Code the Second Template (shows the individual gallery’s thumbs and full sized images)
    This template does a lot of the same things as before. Use glob() to gather all the images inside the folder name passed in the URL. You already know it’s going to be “images/gallery” so just append “/folder_name_from_URL” to the path for glob(). Again, use if statements to remove unnecessary folders AND files. We don’t want to grab that text file, just the images. Next use something like this:

    $handle = fopen("inputfile.txt", "r");
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            // process the line read.
        }
    } else {
        // error opening the file.
    }

    Again, you should know the path is “images/galleries/folder_name_from_URL” and you also know the name of the text file… so the fopen should make sense. Every time you iterate through one line of the text file, explode() that line using a pipe (|) because you are already using the pipe to begin with, and then loop through the glob() results to locate the image in question. Use that information to display the thumbnail and link it to it’s larger counterpart.

    You can also use things like prettyPhoto or imageZoom to make the whole thing a lot prettier.

    I know this looks and sounds like a lot, but believe me it’s really only 20-30 lines of code max. Most of it is repetitive code… Let me know if you need more direction. Do a search on the PHP site for the functions I’ve referenced and you’ll get a lot of examples of how they are used. Some might even be the exact answer you are looking for!

    Thread Starter 39images

    (@39images)

    Hmmm, that sounds like it would work but I’m honestly not familiar with PHP at all. I think what they want is a different gallery on each page, accessible through the main nav. Like you hover over Gallery in the menu and the drop down allows you to choose which one you go to.

    wpismypuppet

    (@wordpressismypuppet)

    This same method I posted will work in a menu as well… instead of a landing page to show all the galleries (which you should probably have when they click on “Gallery” itself anyways) you could just use a custom Walker function to populate the navigation dynamically.

    If you don’t know PHP all that well, it might be time to post in the jobs piece of this forum and get some help. What you want it a little advanced and you won’t get the whole code for free in this forum.

    Sorry for the bad news…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Gallery that pulls captions from text file.’ is closed to new replies.