← WordPress

Atribuir "class" a todas as imagens no post

Lida 2331 vezes

Offline

tartofs 
Membro
Mensagens 205 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Tenth year Anniversary Nineth year Anniversary Windows User Linux User Mobile User Level 5 Super Combination Combination Topic Starter Level 4

Boas, tenho uma questão que até pode ser simples mas eu não encontro solução! lol

 :-?

Eu gostava de atribuir um atributo "class" a todas as imagens do post mas somente do post.

Por exemplo, o meu theme tem uma opção de adicionar imagens com lightbox, mas somente nessa função do theme. Eu consegui colocar um thumbnail com essa classe que contêm a "Featured Image", com o o seguinte código e onde lhe encaixei a "class":

Código: [Seleccione]
<?php 
 
if ( has_post_thumbnail()) {
   
$large_image_url wp_get_attachment_image_srcget_post_thumbnail_id($post->ID), 'large');
   echo 
'<div class="thumbsingle"><a class="image_sc lightbox_img" href="' $large_image_url[0] . '" title="' the_title_attribute('echo=0') . '" >';
   echo 
get_the_post_thumbnail($post->ID'thumbnail'); 
   echo 
'</a></div>';
 }
?>

Como é que adiciono a
Código: [Seleccione]
class="image_sc lightbox_img" a todas as imagens do post?

Tudo isto é porque quero utilizar uma função disponível no theme, ao invés de ter que instalar um plugin para isso.

;)
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

funtions.php


Código: (php) [Seleccione]
function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size, $alt = '' ){
  $classes = 'image_sc lightbox_img';

  if ( preg_match('/<img.*? class=".*?">/', $html) ) {
    $html = preg_replace('/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html);
  } else {
    $html = preg_replace('/(<img.*?)>/', '$1 class="' . $classes . '" >', $html);
  }
  return $html;
}
add_filter('image_send_to_editor','give_linked_images_class',10,8);

Vê se resulta...
Offline

tartofs 
Membro
Mensagens 205 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Tenth year Anniversary Nineth year Anniversary Windows User Linux User Mobile User Level 5 Super Combination Combination Topic Starter Level 4

Boas Pedro,

Muito obrigado pela dica.

Funciona em parte já que está a adicionar o atributo, mas ele dá a "class" à imagem (<img class="xxx") e neste caso para o Lightbox do theme funcionar tem que atribuir ao link -> <a class="xxx">.

 :obrigado:
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Boas Pedro,

Muito obrigado pela dica.

Funciona em parte já que está a adicionar o atributo, mas ele dá a "class" à imagem (<img class="xxx") e neste caso para o Lightbox do theme funcionar tem que atribuir ao link -> <a class="xxx">.


 :obrigado:

Nos dois lugares em que tem <img, muda para <a
Offline

tartofs 
Membro
Mensagens 205 Gostos 0
Troféus totais: 28
Trófeus: (Ver todos)
Tenth year Anniversary Nineth year Anniversary Windows User Linux User Mobile User Level 5 Super Combination Combination Topic Starter Level 4

Boas Pedro,

Funcionou a 100%. Muito obrigado.

 :grin:
Offline

Pedro Lopes 
Beta tester
Mensagens 3568 Gostos 18
Feedback +6

Troféus totais: 32
Trófeus: (Ver todos)
Level 6 Windows User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Boas Pedro,

Funcionou a 100%. Muito obrigado.

 :grin:

De nada :D