Which file should I edit to add my own class to class="attachment-thumbnail" ?

Home Forums Using Thematic ( without a child theme ) Which file should I edit to add my own class to class="attachment-thumbnail" ?

This topic is: not resolved

This topic contains 8 replies, has 3 voices, and was last updated by  middlesister 2 years, 1 month ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #620

    themer
    Participant

    Hi everyone,

    Can someone please tell me which page of TTF should I edit to add my own class to “attachment-thumbnail” class? Basically what I need to do is add new class so I will get this:

    class="attachment-thumbnail lightbox"

    This way my attached images will open in a pop up and not just in the parent window as they do by default.

    Thanks!

    #626

    middlesister
    Moderator

    This is not a thematic question but a general wordpress question. That said, I think I found a filter for you. It’s wp_get_attachment_image_attributes. I think this should work:

    [php]function midsis_add_lightbox_to_attachments( $attr ) {
    $attr['class'] .= ‘ lightbox’;
    return $attr;
    }
    add_filter( ‘wp_get_attachment_image_attributes’, ‘midsis_add_lightbox_to_attachments’ );[/php]

    Untested though.

    #635

    themer
    Participant

    Hey middlesister, thank you so much for answering my post!

     

    One noob question though, in which WP file.php should I add your code?

     

     

     

     

    #637

    helgatheviking
    Moderator

    if you are using thematic, we recommend that you create a child theme.  you can google this plenty while we’re working on docu.  or you can just copy the asamplechildtheme folder right out of the thematic folder and drop it into your /wo-content/themes folder, activate the sample and you’ll be ready to go.  then going forward all PHP functions go your in your child’s functions.PHP and all CSS go in your child’s style.CSS.

    #653

    themer
    Participant

    Ooook, I managed to add this “lightbox” class by replacing the

    [php]‘class’ => "attachment-{$size}"[/php]

    with

    [php] ‘class’ => "attachment-{$size} lightbox"[/php]

    in media.php file.

    However, what I need to do is set this ligthbox class to a href! I found the variable which creates the links but I don’t know how to add the a class=”lightbox” to it ?

    Looks like this:

    [php]$link = isset($attr['link']) && ‘file’ == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);[/php]

    Than there is an output which calls $link, so I guess I should somehow edit the above code to make this $link to have a class=”lightbox” while outputting.

    Any ideas?

    #654

    helgatheviking
    Moderator

    media.php?  just where are you editing?

    most likely, i’d advise you to stop whatever you are doing and create a child theme.   please don’t hack thematic core and definitely, definitely don’t hack wordpress core… unless you are prepared to lose all your work when the next upgrade hits .. or stay stuck locked into outdated code.

    once you  have a proper child theme (if my previous post wasn’t clear there are tutorials available via google until we get our docu in order).  THEN add all your PHP customizations (such as @middlesister‘s code) to your child theme’s functions.PHP and all your CSS customizations to style.CSS

     

     

    #656

    themer
    Participant

    @helgatheviking, I’m aware of what you are talking about. But to be honest, this is really a minor hack compared to all the stuff I had to hack in order to make all the plugins compatible with each other. Updating will be possible though, but with some manual editing, no big deal, really. I usually try not do stuff like that but this time I really had no choice.

    Anyway, any ideas how could I make

    [php]$link = isset($attr['link']) && ‘file’ == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);[/php]

    my anchor class in output?

    #657

    helgatheviking
    Moderator

    just b/c it seems a “minor” hack doesn’t make it any less the wrong way to do things.  so what is the reason you won’t try middlesister’s code?

    #668

    middlesister
    Moderator

    Helga is right. Do not edit any of the core code. Ever. Period. One ‘minor hack’ easily becomes two or three and you’ll have a headache trying to remember what went where. And when something gets messed up you don’t know if it is wordpress or your edits that is acting up. Not worth it. Especially when such a small filter will do what you need. In this case you really had a choice: use a filter.

    Are you using Thematic at all? I don’t recognize that piece of code.

    If you need to add the lightbox class to the anchor element, there is no way of doing it with editing that line of code anyway. You need to look into filtering wp_get_attachment_link.

    [php]function midsis_add_lightbox_to_attachmentlink( $link ) {
    $link = str_replace( ‘title=’, ‘class="lightbox" title=’, $link);
    return $link;
    }
    add_filter( ‘wp_get_attachment_link’, ‘midsis_add_lightbox_to_attachment’ );[/php]

    It’s very worthwhile to learn how to use filters and hooks properly. There is very little you can’t do with them.

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Which file should I edit to add my own class to class="attachment-thumbnail" ?’ is closed to new replies.