I am using a WordPress site and wanted to integrate photo captions and photo credit line under the fatured image. I found a great code that adds a Photographer credit and URL field to the media upload page. However, upon displaying it in the page, I want to further customize it.
Currently, when i upload a photo, i can add a photographer name, and a photographer (or source) URL. The URL automatically links to the name if filled in. However, what I want to do is add a text and icon before the photographer name to say "(icon) Image Credit / "
I was able to add it using css, however, when there is no photographer name, it still shows the Image Credit text and icon but no name. How can I hide that when there is no value in the photographer name field?
also, if i enter a photographer name, but no URL, it links the name to an empty URL. How can i make it so that it does not link the name if the URL field is empty?
Here is the code i am working with:
function be_attachment_field_credit( $form_fields, $post ) {
$form_fields['be-photographer-name'] = array(
'label' => 'Photographer Name',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'be_photographer_name', true ),
'helps' => 'If provided, photo credit will be displayed',
);
$form_fields['be-photographer-url'] = array(
'label' => 'Photographer URL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
'helps' => 'Add Photographer URL',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );
/**
* Save values of Photographer Name and URL in media uploader
*
* #param $post array, the post data for database
* #param $attachment array, attachment fields from $_POST form
* #return $post array, modified post data
*/
function be_attachment_field_credit_save( $post, $attachment ) {
if( isset( $attachment['be-photographer-name'] ) )
update_post_meta( $post['ID'], 'be_photographer_name', $attachment['be-photographer-name'] );
if( isset( $attachment['be-photographer-url'] ) )
update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
Then in my single.php file I added the following code to display the photo credit.
<div id="tgg_credit_line" class="tgg-photo-credit" align="right">
<?php echo "📷 Image Credit / " ?><?php echo get_post_meta(get_post_thumbnail_id(), 'be_photographer_name', true); ?>
</div>
You can first check to see if there is a photographer name set, and if not, don't add any of the credit code. You can also check whether there is a url and then only add the link tag when necessary. This code should be used in place of what you have in the single templates.
<?php
$photographer_name = get_post_meta(get_post_thumbnail_id(), 'be_photographer_name', true);
$photographer_url = get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true);
if ( $photographer_name ) : ?>
<div id="tgg_credit_line" class="tgg-photo-credit" align="right">
📷 Image Credit /
<?php if ( $photographer_url ) : ?>
<a href="<?php echo $photographer_url ?>">
<?php endif; ?>
<?php echo $photographer_name ?>
<?php if ( $photographer_url ) : ?>
</a>
<?php endif; ?>
</div>
<?php endif; ?>
Related
I'm trying to amend a Wordpress template so that the thumbnail of a single post links to an external url. I have collected the url in a custom field (report_link).
I've tried a bunch of combinations of dots and apostrophes and speech quotes, but I think I must be missing something fundamental.
<?php
if ( has_post_thumbnail() ) {
$replink=the_field('report_link');
if( $thumbnail = get_the_post_thumbnail( null, 'slider', array( 'class' => 'img-fluid' ) ) ){
echo "<a href='$replink'>$thumbnail</a>";
}
}
?>
When I try to use $replink in the href the link on the thumbnail is back to the same post's url.
I also tried:
echo 'Click';
This returned the url in $replink printed to the screen and then a the word 'Click' which was linked back to the post page containing the link.
You can make use of below function to make it work.
if ( has_post_thumbnail() ) {
$replink = get_field('report_link');
if( $thumbnail = get_the_post_thumbnail( null, 'slider', array( 'class' => 'img-fluid' ) ) ){
echo "<a href='".$replink."'>".$thumbnail."</a>";
}
}
I tried using suggestions from other posts but they do not work. I am looking to not show the featured product image in my product images area/image gallery because I am using the featured image in the header as a background image and its too wide to be in the gallery.
So far this is the closest thing to working but I get an error. It does however do what I need.
Any way to fix this so i do not get the error?
Here is my code:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);
function remove_featured_image($html, $attachment_id, $post_id) {
$featured_image = get_post_thumbnail_id($post_id);
if ($attachment_id != $featured_image) {
return $html;
}
return '';
}
And here is the error:
Missing argument 3 for remove_featured_image() in /home/…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
Warning: Missing argument 3 for remove_featured_image() in /home…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
There is only 2 arguments for woocommerce_single_product_image_thumbnail_html filter hook.
So you have to change a little bit your code to avoid the error, this way:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
function remove_featured_image($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
References for filter hook woocommerce_single_product_image_thumbnail_html:
woocommerce templates: single-product/product-image.php
woocommerce templates: single-product/product-thumbnails.php
This is what I did, hope it's helpful.
I used the Meta Box plugin to create a custom checkbox in the product edit page (in the Wordpress backend). The checkbox is basically "Hide featured image from product gallery" and here's the code (to be placed in the functions.php file):
function hide_first_img( $meta_boxes ) {
$prefix = 'meta_box';
$meta_boxes[] = array(
'id' => 'hide_img',
'title' => esc_html__( 'Hide first image', 'custom_meta_boxes' ),
'post_types' => array('product'),
'context' => 'advanced',
'priority' => 'default',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . '_hide_first_image',
'name' => esc_html__( 'Hide first image in product gallery', 'custom_meta_boxes' ),
'type' => 'checkbox',
'desc' => esc_html__( 'Check to hide the first image in the gallery', 'custom_meta_boxes' ),
),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'hide_first_img' );
Then I used LoicTheAztec's code, which works great, to remove the image ONLY if the checkbox is checked, like this (to be placed in the functions.php file):
function remove_featured_image($html, $attachment_id ) {
if( rwmb_meta( 'meta_box_hide_first_image' )){
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
}
return $html;
}
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
Finally, only for FLATSOME THEME users, to also hide the first thumbnail in the gallery, I directly edited the following file in my child theme folder: \flatsome-child\woocommerce\single-product\product-gallery-thumbnails.php
Maybe there's some similar file for the default Woocomerce gallery or for other themes:
// show first thumbnail only if meta box is NOT checked
<?php if( !rwmb_meta( 'meta_box_hide_first_image' )) : ?>
<div class="col is-nav-selected first">
<a>
<?php
$image_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src( $image_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_'.$image_size ) );
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$image = '<img src="'.$image[0].'" alt="'.$image_alt.'" width="'.$gallery_thumbnail['width'].'" height="'.$gallery_thumbnail['height'].'" class="attachment-woocommerce_thumbnail" />';
echo $image;
?>
</a>
</div>
<?php endif; ?>
I can confirm that this is working live on my site right now.
I have created a very simple plugin for my Wordpress site to display links to my most recent posts with the use of a shortcode ([recentposts]). The plugin works so far but I am struggling with finding a way to display the featured image for each post that is called in the <div> tags for each post link.
Can you please advise how I may do this. The code for my plugin is as follows:
<?php
/*
Plugin Name: Blog Display Blocks
Description: Plugin to display blog posts in block with shortcode
Author: Chris Brosnan
*/
function RecentPosts() {
$recent_posts = wp_get_recent_posts(6);
echo '<div class="blog-contain">';
foreach( $recent_posts as $recent ){
echo '<div class="third-box">' . $recent["post_title"].' </div> ';
}
};
echo '</div>';
add_shortcode('recentposts', 'RecentPosts');
register_activation_hook( __FILE__, array( 'Blogdisplay', 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( 'Blogdisplay', 'plugin_deactivation' ) );
?>
What would I need to do in order to show the featured image alongside the corresponding link for each post that is called?
Through trial and error combined with some further searching I have now found the solution to my question.
<?php
/*
Plugin Name: Blog Display Blocks
Description: Plugin to display blog posts in block with shortcode
Author: Chris Brosnan
*/
add_image_size( 'featured-thumb', 300, 200, true ); // (cropped)
function RecentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=6');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'featured-thumb' ); ?>
<?php $image_url = $image[0]; ?>
<a href="<?php the_permalink(); ?>">
<div class="third-box" style="background-image: url(<?php echo $image_url; ?>);">
<p><?php the_title();?></p>
</div>
</a>
<?php endwhile;
wp_reset_query();
};
add_shortcode('recentposts', 'RecentPosts');
register_activation_hook( __FILE__, array( 'Blogdisplay', 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( 'Blogdisplay', 'plugin_deactivation' ) );
?>
Within your code by using wp_get_recent_posts() you will get the post id($recent["ID"]), then you can use any one of following,
just add this in your code where you want to show the featured image.
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $recent["ID"] ), 'single-post-thumbnail' );
or can use
echo get_the_post_thumbnail($recent["ID"], 'featured-image');
I've added multiple featured images to my wordpress site using the multiple post thumbnail plugin. I'm trying to display them all underneath the content with their descriptions. I can do it for the main featured image no problem. I can display the rest of the featured images no problem, but whenever I try to add description by it's the page description not the image description.
This is how I added the main image and description.
<?php the_post_thumbnail( 'product-thumbnail' );
echo get_post(get_post_thumbnail_id())->post_content; ?>
The remaining images are added as such:
<?php MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-
image', NULL, 'product-thumbnail');
?>
And so forth (third, fourth)..
Can somebody help with how to add the descriptions for the rest?
To display your post thumbnail with its caption, simply paste the following code inside the loop:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
You can also display entire image description by adding this code inside the post loop:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_content; ?>
the above codes for single images if you use multiple images use this code below
<?php
$the_post_images = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type'=> 'image'
) );
foreach ($the_post_images as $the_post_image) {
// SHOW FEATURED IMAGE TITLES
echo get_the_title( $the_post_image->ID );
//SHOW IMAGE DESCRIPTION OR CAPTIONS
echo apply_filters( 'get_the_excerpt', $the_post_image->post_excerpt );
}
?>
To get image, its caption in multiple post thumbnails,
`
echo $secondimgPath = MultiPostThumbnails::get_post_thumbnail_url( get_post_type(), 'second-featured-image', NULL);
echo $secondimgIdAttachment = abcd_get_attachment_id_by_url($secondimgPath);
$size = array( 854,395, 'bfi_thumb' => true, 'quality' => 100);
echo $secondLargeImage[0] = wp_get_attachment_image( $secondimgIdAttachment,$size );
echo get_post( $secondimgIdAttachment )->post_excerpt;
endif; ?>`
In functions.php, use the following -
function abcd_get_attachment_id_by_url( $url ) {
// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );
// Get the host of the current site and the host of the $url, ignoring www
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
$file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
// Return nothing if there aren't any $url parts or if the current host and $url host do not match
if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
return;
}
// Now we're going to quickly search the DB for any attachment GUID with a partial path match
// Example: /uploads/2013/05/test-image.jpg
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );
// Returns null if no attachment is found
return $attachment[0];
}
i am working on a homepage which includes post, sometimes the post is only with featured image and no content, and sometime there is post with both featured image and content. i want to show excerpt with the post with a readmore button when there is content available with the post. i have added a read more button but it even shows with the post which don't have any content. how can i show it only when there is excerpt available for content. here is the code for the excerpt div.
<div class="entry-excerpt">
<?php
if( strlen( $post -> post_excerpt . $post -> post_content ) > 0 ){
?>
<div class="excerpt">
<?php
if( is_user_logged_in () ){
the_excerpt();
echo 'Continue Reading';
}else{
$meta = meta::get_meta( $post -> ID , 'settings' );
if( isset( $meta['safe'] ) ){
if( !meta::logic( $post , 'settings' , 'safe' ) ){
the_excerpt();
echo 'Continue Reading';
}
}else{
the_excerpt();
}
}
?>
</div>
<?php
}
?>
</div>
the question is solved...
here if anyone want to need it in future.
just add this code in functions.php
<?php
function new_excerpt_more($more) {
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>'; } add_filter('excerpt_more', 'new_excerpt_more'); ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
The $more_link_text sets the link text like "Read More". The second one, $strip_teaser, sets whether or not the "more" link should be hidden (TRUE) or displayed (FALSE). The default is FALSE, which shows the link text.
Source