Omit image captions from get_the_content()

I use a theme that calls get_the_content() to display short excerpts of the latest blog posts on the home page. Now I noticed that the excerpt sometimes starts with an image caption, if the blog post contains a picture at the very beginning. I usually do that and make the text float around it. I always set the first three caption fields on an image (Alt Text, Title, Caption). Is there any way to strip these image captions from the …
Category: Web

Place image caption outside of figure tag (Gutenburg Image Markup)

I format my single.php images to a perceived 3:2 ratio like this: <figure style="position:relative; width:100%; padding-bottom:66%;"> <img style="position:absolute; width:100%; height:100%; object-fit:cover;"> <figcaption>some text</figcaption> </figure> With Gutenberg, this sticks my caption behind my image or on top of my image (if I position figcaption relative or absolute). I'd like it to be below my image. Before gutenberg, I could customize my image markup. No longer can. Now I am trying to figure out how to make my image caption the next …
Category: Web

Remove Captions from Custom Gallery function.php

I like to prevent captions from being outputted in my custom gallery function. I could easily just hide it using CSS but it doesn't feel right. The caption is being sent from media.php this line. if ( $captiontag && trim($attachment->post_excerpt) ) { $output .= " <{$captiontag} class='wp-caption-text gallery-caption'> " . wptexturize($attachment->post_excerpt) . " </{$captiontag}>"; } I could just remove . wptexturize($attachment->post_excerpt) . but as soon as I update its gone. Id like to to this without copying the whole gallery …
Category: Web

Dynamically adding Captions to images

I have a site where the author has included Amazon Affiliate links. They've done this by adding the URL to product images. I'm wondering if there's a way to add a function that will automatically add a wp-caption below these images if they're linked to Amazon. Basically the condition would need to be: if an href containing an image is linked to a domain including 'amazon', add a caption below that image that says "Buy Now". The caption would also …
Category: Web

Forcing max-width for image captions

I've implemented a filter to my image captions based on this question I asked a few days ago. How can I slip into the caption the css style of max-width as well? I think it can be done with preg_replace, but I can't get it to work. All I really am trying to do is force the image caption text to wrap inside the div. The default html output looks like... <div id="attachment_1071" style="width: 310px" class="wp-caption alignright"> I need it …
Category: Web

how automatically show the image title before the image caption/description in a wordpress gallery?

I am using a simple Wp 5.x gallery like this [gallery size="full" columns="1" ids="6304,6305,6306,6307,6308,6309"] The gallery shows me the images and the captions/description after the image. How can I automatically insert/inject the gallery image title BEFORE this image captions/description? See image expample attached Thanks in advance
Category: Web

Wordpress Caption Formatting

I'm trying to create a custom wordpress theme. Everything has gone fine, except image formatting in post content. Image captions are not displaying properly in newly created theme. I have already added wordpress core css as given below. .alignnone { margin: 5px 20px 20px 0; } .aligncenter, div.aligncenter { display: block; margin: 5px auto 5px auto; } .alignright { float:right; margin: 5px 0 20px 20px; } .alignleft { float: left; margin: 5px 20px 20px 0; } a img.alignright { float: …
Category: Web

How to modify image caption in posts?

I am trying to modify the caption of images in posts by filtering img_caption_shortcode, and then displaying the content using the_content(). I want to add bylines/credit on images, so I have created two fields in ACF that should be added inside the <figcaption> on all my posts. Basically I want to do what is description in the blog. So from this: <figcaption> So this is a caption that describes the image. </figcaption> To this: <figcaption class="post-thumbnail-caption"> So this is a …
Category: Web

Gallery IDS and Captions: hide/show on a per ID basis

I am aware of hiding captions in a gallery via css on figcaption, like .wp-caption-text/.gallery-caption for example. What I am looking for is a way to have custom control of hiding captions on specific images in an array of gallery ids: [gallery ids="2206,1910,1377"] I have no idea how to pass a second array, but I was wondering if a solution might work that way. Concept Example: [gallery caption-toggle="y,n,n" ids="2206,1910,1377"] If something like that is workable, then I'm assuming the loop …
Category: Web

make Wordpress image captions responsive

This webpage contains images inserted by Wordpress. The code used to insert the first image is: [caption id="attachment_887" align="alignnone" width="604"] <a href="http://steven.doig.com.au/files/2013/06/Forest_Legacy_m.jpg"> <img class="size-large wp-image-887" alt="a Forest Legacy group" src="http://steven.doig.com.au/files/2013/06/Forest_Legacy_m-1024x681.jpg" width="1024" height="681" /> </a> a Forest Legacy group[/caption] This image is controlled by CSS: #content .wp-caption a img { width: 614px; height: auto; } I want to make this image responsive. I've inserted the CSS: @media (max-width:988px) { #content .wp-caption a img { width: 99.03225806%; /* 614/620 */ height: auto; …
Category: Web

How to Display Image Caption but Not Alt Text

I'm trying to display images with their caption, but when I do it displays both the caption and that alt text. I've tried to use unset( $fields['image_alt'] ); to hide the alt text, but it does nothing. Here's my code: <?php if (the_post_thumbnail_caption()) { the_post_thumbnail_caption(); } function remove_alt( $fields ) { unset( $fields['image_alt'] ); return $fields; } add_filter( 'attachment_fields_to_edit', 'remove_alt', 999, 1 ); ?> It first gets the caption, if there is one, and then the function is supposed to …
Category: Web

Remove inline width from figure

I add an image with caption to my page and need to increase <figure> width. But wordpress is adding an inline width. [caption id="attachment_44" align="alignnone" width="216"]<a href="image path"><img src="image path" alt="caption" width="216" height="95" class="size-full wp-image-44" /></a> caption [/caption] So, the DOM look like this: <figure id="attachment_43" aria-labelledby="figcaption_attachment_43" class="wp-caption alignnone" style="width: 216px"> <a href=""> <img src="http://localhost/bordados/website/wp-content/uploads/2014/02/homeThumb01.jpg" alt="Caption" width="216" height="95" class="size-full wp-image-43"> </a> <figcaption id="figcaption_attachment_43" class="wp-caption-text">Caption.</figcaption> </figure> I try to remove width="216" from code, but the caption desappear. How I remove this …
Category: Web

Get Image Description

I'm trying to make a post split in 2 columns. The first and left one is going to be any image inside the post, and the second and right one will be the_content() (excluding the images). So as of now I have no problem pulling all the images. However - I can't seem to get the image caption or title or description. Heres my code: <?php if ( $images = get_posts(array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'numberposts' => -1, …
Category: Web

How to add a link in a image's caption?

I would like to add a link to the caption of a photo in one of my posts. I can type in the HTML for the link in the caption, but when I publish the post the link gets removed. How do you add a link to a caption? This would be really useful for giving photo credit.
Category: Web

Add post-thumbnail after first paragraph including the caption

The function below is used to show the post thumbnail after the first paragraph. add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $content ) { global $post; if (has_post_thumbnail($post->ID)) { $caption = '<span class="image-caption">'.get_the_post_thumbnail_caption( $post ).'</span>'; $img = '<p>'.get_the_post_thumbnail( $post->ID, 'full' ).'</p>'; $content = preg_replace('#(<p>.*?</p>)#','$1'.$img . $caption, $content, 1); } return $content; } (Thanks to Add 'if exists' to filter) I have modified it in order to display the caption for the image. However, if there isn't a caption for the …
Category: Web

Can I nest a <figcaption> element within <a> tags from default wordpress?

I am making a page with Gutenberg but there is a problem that the link does not work within a . This is the code when I inspect: &lt;figure&gt; &lt;a href="..."&gt; &lt;img&gt; &lt;/a&gt; &lt;figcaption&gt; &lt;/figcaption&gt; &lt;/figure&gt; Is there some way that I could nested the figcaption within the a tags so it could be clickable ? I can not modify the code because it is generated by Wordpress. The expected output: &lt;figure&gt; &lt;a href="..."&gt; &lt;img&gt; &lt;figcaption&gt; &lt;/figcaption&gt; &lt;/a&gt; &lt;/figure&gt;
Category: Web

How to have different captions for same image, for galleries?

Seems like I can't have the same image in different galleries with different captions. Is it possible to make that working in any way? Like if we could change the shortcut code that Wordpress is creating when you insert a gallery. Maybe to something like this: [gallery] [image 244 caption="Caption 1"] [/gallery] [gallery] [image 244 caption="Caption 2"] [/gallery] The best would be to get this working with Wordpress's own gallery functionality. So when you click "Insert gallery", the above code …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.