- This topic has 32 replies, 2 voices, and was last updated 3 years, 6 months ago by Leo.
-
AuthorPosts
-
April 17, 2015 at 12:51 pm #99618Emily Condit
I figured out the parse error. It was putting in #39; for the single quotes. I changed those to real single quotes and the parse error is gone. However, there is no change in the effect of the function. It’s still duplicating images and there is no caption under my featured image in my post.
cafeemily.com
cafeemily.com/the-voice-top-12-season-8/The first link is my home page. You can see the duplicate images.
The second link is the post where I put a caption on the featured image but it’s not showing up in the post itself.
I really appreciate your help. You probably have a life besides this forum and I hope I’m not taking up too much of your time.
Thanks!
π EmilyApril 17, 2015 at 9:30 pm #99704Emily ConditI found a fix. Will post it later.
April 17, 2015 at 10:52 pm #99711TomLead DeveloperLead DeveloperSorry about the error – looks like I need to start embedding code in a different way so no weird characters get inserted.
Looking forward to hearing about the fix π
April 18, 2015 at 3:04 pm #99921Emily ConditTHE FIX
Okay, so what I wanted to do was to insert the image with the copyright AFTER the entry header on my main page where masonry is enabled and BEFORE the entry header on the single post page for the linked post.
I looked at the notes in your code and you had a conditional that said if(!is_singular() && !is_404()) which means the code would only apply to posts that are either in masonry blocks or on a page where multiple posts are listed one after the other.
The plugin you talked about takes care of that issue without any additional code (as far as I can tell anyway. I have masonry enabled and the copyrights appeared under the featured thumbnails without any additional code). The thing the plugin DOESN’T do, at least in conjunction with this theme, is to add the copyright under featured images on single posts.
I’m not sure HOW to remove the featured images on single posts. The code you gave doesn’t remove them. If you know how to remove the featured image on a single post, I can change the code in my functions.php so I don’t need extra CSS to hide it.
So the full fix for what I needed to do is to download the plugin you mentioned which took care of the copyright on the thumbnails, modify your code so that it only operated on single posts using the before_content hook instead of the after_entry_header hook, and adjust the CSS to hide the featured image and remove the margin from the top of the single post image so that it was flush with the top widget of my sidebar.
Here’s the plugin link you gave us (posting for those who might want the full fix in one topic post): https://wordpress.org/plugins/featured-image-caption/
***This is an aside from the full fix***
This plugin either has some errors or doesn’t work the way it’s supposed to with your theme. In the PHP code, the div for the caption APPEARS to be built correctly but when in use, does not actually display the way it’s built. But fortunately, this only really matters if you want to modify the CSS of the caption text separately from the source. There is a workaround, however. These rules will allow you to modify the caption text separately from the caption source:.cc-featured-image-caption-text, .cc-featured-image-caption-text a {
/*put all rules for the caption SOURCE here*/
}.cc-featured-image-caption {
/*put all rules for the caption TEXT here*/
}
***end aside. resume full fix***Next, I modified Tom’s code as follows. This is my full functions.php file in the blank child theme for Generate Press:
<?php /** * Generate child theme functions and definitions * * @package Generate */ add_action('after_setup_theme','generate_remove_blog_post_image'); function generate_remove_blog_post_image() { //this is removing the generate_before_content because on a single post, I want my image to appear BEFORE the entry header. This code does not remove a single post featured image. Tom, if you know how to remove a single post image, please, let me know the code. remove_action( 'generate_before_content', 'generate_blog_post_image' ); } //this adds the image with the post with its caption before the entry header add_action( 'generate_before_content', 'generate_post_image_caption' ); function generate_post_image_caption() { // If there's no featured image, return if ( ! has_post_thumbnail() ) return; //Modified. This is checking for a post that IS a single post and NOT a 404 if ( is_singular() && ! is_404()) { ?> <div class="post-image"> <a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'full', array('itemprop' => 'image') ); ?><?php if ( function_exists( 'cc_featured_image_caption' ) ) : cc_featured_image_caption(); endif; ?></a> </div> <?php } }
Finally, this added to the custom style sheet of the theme (I’m using Jetpack per Tom’s suggestion) will remove the featured image from a single post:
.inside-article .page-header-image-single {
display: none;
}This CSS pulls the single post image up so it’s flush with the top of the widget sidebar(s) and gives some breathing room between the caption and the entry header:
.inside-article .post-image {
margin-bottom: 20px;
margin-top: 0;
}I hope that will be helpful to someone. I would still like to know the php code to remove a single post featured image though.
Thanks, Tom.
You rock.
π Emily
April 19, 2015 at 11:17 pm #100266TomLead DeveloperLead DeveloperHi Emily,
Great job finding a way to get it to work! Thanks so much for sharing so others can make use of it π
May 30, 2015 at 1:48 pm #111491PaulHi,
these solutions over there do not work. I do not know why.
But I am glad to find another solution.
For functions.php:
function the_post_thumbnail_caption() {
global $post;$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array(‘p’ => $thumbnail_id, ‘post_type’ => ‘attachment’));if ($thumbnail_image && isset($thumbnail_image[0])) {
echo ‘‘;
}
}For example content-single.php:
<?php
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
the_post_thumbnail();
if(!empty($get_description)){//If description is not empty show the div
echo ‘‘;
}
?>May 31, 2015 at 12:06 am #111539TomLead DeveloperLead DeveloperThanks for sharing! The above code most likely won’t work for others as posting it into the forum without the “code” tags convert the apostrophes and other special characters. It would be great if you could post it to a pastebin? http://pastebin.com/
Thanks again! π
August 22, 2015 at 10:25 pm #131465ReubenHi Tom, I switched from the Twenty Fifteen theme to GeneratePress Premium a while back and realise my featured image captions are gone when displaying a single page.
What now is the best solution for displaying the captions? I don’t see why we need to go through the process of hiding the original image and replacing it with something else… it just feels convoluted.
August 22, 2015 at 10:36 pm #131472ReubenOkay, I modified Emily’s code a little to make it simpler. Basically you don’t even need to remove the featured image, but rather append the caption below it. Just inserting the following into functions.php will do.
add_action( 'generate_before_content', 'generate_post_image_caption' ); function generate_post_image_caption() { // If there's no featured image, return if ( ! has_post_thumbnail() ) return; //Modified. This is checking for a post that IS a single post and NOT a 404 if ( is_singular() && ! is_404()) { ?> <div style="margin-top: -2em; margin-bottom: 1em; text-align: center"><?php if ( function_exists( 'cc_featured_image_caption' ) ) : cc_featured_image_caption(); endif; ?></a></div> <?php } }
BUT for my archive pages, be it masonry or otherwise, the whole caption is hyperlinked into my post. Is there a way to change this behaviour?
You can see an example here: https://www.buyfromwhere.com/database/retailers/
Scroll down to Sim Lim Square and you’ll see that ‘Image Source’ is hyperlinked even though I don’t want it to be that way.August 23, 2015 at 2:38 am #131494Emily ConditHey, your site looks great. I can’t see the caption though so you might want to consider using a few CSS rules to change the color of the links/text inside your captions if you’re going to put the code inside the image itself which it looks like you did using an inline style.
The cc featured image caption plugin puts the link to your page around the title of your image. I don’t know how to get rid of it other than mess with the plugin code which is a bad idea because they do update their code.
Maybe Tom has an idea. I am going to try out your code to see if it works for me because I don’t like removing the featured image. Thanks for the update.
π Emily
August 23, 2015 at 9:19 am #131528ReubenThanks for telling me about the text inside the caption. It was unintentional; I added the negative margin so that the caption would be brought closer to the featured image in single posts, but I failed to consider the post excerpt.
Here is my revised code:
add_action( 'generate_before_content', 'generate_post_image_caption' ); function generate_post_image_caption() { // If there's no featured image, return if ( ! has_post_thumbnail() ) return; //Modified. This is checking for a post that IS a single post and NOT a 404 if ( is_singular() && ! is_404()) { ?> <?php if ( function_exists( 'cc_featured_image_caption' ) ) : ?><div class="singlecap"><?php cc_featured_image_caption(); ?></div><?php endif; ?> <?php } }
In style.css, add this:
.singlecap .cc-featured-image-caption { margin-top: -2em; margin-bottom: 1em; text-align: center; } .singlecap .cc-featured-image-caption:empty { display: none; margin: none; }
Now, everything should work perfectly. If there is no image caption for that post, the post will appear as per normal.
August 23, 2015 at 9:29 am #131532TomLead DeveloperLead DeveloperLooks awesome! Thanks for sharing all of your code π
As for the hyperlink, you may need to ask the developer of that plugin if there’s a way to disable it without modifying core code – there should be.
July 17, 2017 at 9:49 pm #350982NickI added the following code to my functions.php file.
add_action( 'generate_before_content', 'generate_post_image_caption' ); function generate_post_image_caption() { // If there's no featured image, return if ( ! has_post_thumbnail() ) return; //Modified. This is checking for a post that IS a single post and NOT a 404 if ( is_singular() && ! is_404()) { ?> <?php if ( function_exists( 'cc_featured_image_caption' ) ) : ?> <?php cc_featured_image_caption(); ?><?php endif; ?> <?php } }
Now, the featured image caption displays, but is displayed relative to the body container rather than the page-header-image-single div container. I noticed that the DIV for the featured image caption is not inside the page-header-image-single div. Here is an excerpt from my CSS.
div.page-header-image-single { position: relative; margin: 0; width: 100%; } div.ccfic { position: absolute; bottom: 16px; right: 5px; }
After fooling around with CSS, I added “position: relative;” to the inside_article div and messed around with the positioning on the ccfic div to get the featured image caption to position correctly. It’s not the most elegant solution, but it works.
July 17, 2017 at 11:46 pm #351016TomLead DeveloperLead DeveloperThat’s exactly how I would have done it π
November 8, 2019 at 4:00 am #1056755Capital LinkI add the php code from Nick post to functions.php but I cannot see the caption.
I have generatepress_child theme and as I can understand content-single.php is the post item. Right?
-
AuthorPosts
- The topic ‘Caption for post thumbnails’ is closed to new replies.