简体   繁体   中英

How do I display a text box over a specific class of images in Wordpress?

On my site I have an image class for external images. Every image MUST use the [caption] shortcode. I want to place a border and text box over every image in that class throughout the whole site.

So far I have...

.img-external {
     border: 5px solid #818a99;
     height: auto;
     width: 100%;  
     position: relative;
}

...shows up fine.

I want to add...

    .text-block {
        position: absolute;
        bottom: 20px;
        right: 20px;
        background-color: black;
        color: white;
        padding-left: 20px;
        padding-right: 20px;
}

...with the text "EXTERNAL IMAGE"

This works...

if ($('.img-external')[0]) {
    $('.text-block').text('EXTERNAL IMAGE');
} else {}

...

<div class="img-external">
  <div class="text-block"></div>
</div>

...in theory when I plug it all into codepen but how can I make this work on Wordpress?

Edit: Wordpress is using <figure><img class="img-external" src=""> etc., and I can't get it to work. Where do I call the text-block?

you have to edit yours template js and css file. Also you can add your file. More information you can find here: https://developer.wordpress.org/themes/basics/including-css-javascript/ and here: https://www.wpexplorer.com/javascript-wordpress/

Cheers

change this:

if ($('.img-external')[0]) {
    $('.text-block').text('EXTERNAL IMAGE');
} else {}

to this:

if ($('.img-external')) {
        $('.img-external').first().find('.text-block').text('EXTERNAL IMAGE');
    } else {}

If you want add text only for first div with .img-external class

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM