WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] question: image id (3 posts)

  1. arminbw
    Member
    Posted 6 years ago #

    Is there an easy way to give every image in a post it´s own css id?

    If the first image always would have id "post1-image1", the second id "post1-image2", etc. it would be easy to write a small java-script image-switching-gallery.

    If not, what is the correct WordPress way to access the different images of a post (using php or template tags)?

    thank´s,
    Armin

  2. Roger Theriault
    Member
    Posted 6 years ago #

    You didn't say which version of WordPress. If it's 2.5, view the source of a page and you'll see the images already have a few classes. One looks like "wp-image-nn" where nn is (guessing here...) the id of the image in your posts table. So you don't have to do a thing! Unless you wanted them to have a different naming format.

    Take a peek at the posts table in your database, you'll find that images uploaded into a post are listed in there, they just have a different type. You could write a query to get them, or poke around in the wp-includes directory for the code that does this to display them in your posts.

    BUT, that said, since you mentioned css ids, I am assuming you'll start by displaying them all in your post in the HTML.

    So, if you just want to post a batch of images and then on the client side hide all except one and rotate through with JavaScript, you CAN select them with jQuery fairly easily. And manipulate them. Without individual ids. Check it out before you write a lot of JavaScript.

  3. arminbw
    Member
    Posted 6 years ago #

    Writing a plugin to get my own "the_content" was something I was thinking of, but I now decided to stay as much as possible in the theme-layer.

    Without giving every image it´s own id, I am now using getElementById combined with getElementByTagName in a very very tiny javascript
    (And yes, jQuery might be the modern way to do this).

    Thank´s for your hints!
    Armin

Topic Closed

This topic has been closed to new replies.

About this Topic