Using a variable to an external url in a php href - php

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>";
}
}

Related

Hiding empty values and Linking values

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; ?>

Display Thumbnails Description with Multiple Post Thumbnails

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];
}

Echo single image attachment title in wordpress

I was wondering what is the exact code to be able to get the title on the attachment image page. Here is the code I have right now in my image.php file for the title portion.
<h2><?php echo get_the_title($post->post_parent); ?> <?php echo ' » $attachment->post-title'; ?></h2>
So I want it to show the main post title then the double right arrow and then the single image title but the $attachment->post-title is not the correct command for this.
I am trying this on my test site. Here you can see the exactly what I am referring to. http://pandafeed.net/gave-her-a-bath-and-tucked-her-in-she-passed-out-right-away/thumbnail-for-1407/
Thanks in advance
This should fetch the title for each post's first attached image on the front page (or any post that's part of a list):
<?php
if (! is_singular()) {
$image_id = get_the_ID();
// Not necessary, but it's an option:
//$permalink = get_permalink($image_id);
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_parent' => $image_id
);
$attachments = get_posts($args);
if ($attachments) {
$title = get_the_title($attachments[0]->ID);
print $title;
}
}
?>

How can I get an automatic thumbnail from the first image in a Wordpress post?

I'm creating a Wordpress theme and I'd like to grab the first image in a post as a thumbnail to use in the Facebook's OG meta tag.
I tried using the function get_the_post_thumbnail() but it generates an html img element. Also I'd like to take the first image in the post, without the need of adding a featured image when creating the post.
This should be simple because there are already all thumbnails generated for every post, I'm just not getting it right.
Here I made some function for you that you can hook to add/edit attachment event.
function set_first_as_featured($attachment_ID){
$post_ID = get_post($attachment_ID)->post_parent;
if(!has_post_thumbnail($post_ID)){
set_post_thumbnail($post_ID, $attachment_ID);
}
}
add_action('add_attachment', 'set_first_as_featured');
add_action('edit_attachment', 'set_first_as_featured');
There is a lot of space for improvement, but this one works like a charm too. On every upload / edit attachment, function checks if the post already has featured image. If it has not, image in question is set as featured. Every next picture will be ignored (since post already has featured image).
Maybe someone finds it helpful (you found solution in the middle of my coding, so... :) )
I found this solution:
$size = 'thumbnail'; // whatever size you want
if ( has_post_thumbnail() ) {
the_post_thumbnail( $size );
} else {
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 1)
);
foreach ( $attachments as $thumb_id => $attachment ) {
echo wp_get_attachment_image($thumb_id, $size);
}
}
I found a solution:
wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail' )[0];
It works fine.
Put this code in your theme's functions.php:
// make the first image of WordPress post as featured image
function first_image_as_featured() {
global $post, $posts;
$first_img_featured = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img_featured = $matches [1] [0];
if(empty($first_img_featured)){ //Defines a default image
$first_img_featured = "/images/default.jpg";
}
return $first_img_featured;
}
Then add the below code inside WordPress loop:
<?php
if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail(); ?>
<?php }
else { ?>
<img src="<?php echo first_image_as_featured(); ?>" />
<?php
}
?>
If the featured image not set, it will automatically take the first image as featured image.
Source: Get The First Image Of WordPress Post As Featured Image
I have tried the solutions above withou success. So, I build a new and easy solution:
function set_first_as_featured($post_id){
$medias = get_attached_media( 'image', $post_id );
if(!has_post_thumbnail($post_id)) {
foreach ($medias as $media) {
set_post_thumbnail($post_id, $media->ID);
break;
}
}
}
add_action('save_post', 'set_first_as_featured');
When you save a post, this code will check if it has any thumbnail. If not, so it will set the first image attached to this post as thumbnail.

URL Variable of Native Magento customization tool uploader to preview the image?

In magento customization tool, you can put file upload option
but I would like that When you upload an image preview in the screen at least when you click on edit the product
which is the variable of the loaded image?
With The URL of the cart image link:
htt....MY-WEB.com/sales/download/downloadCustomOption/id/107/key/a5cae363d3d6cde2e9c6/
I test with:
img src=..
this URL and display ok, but which is the variable that takes this to interfere with an echo
Here they do with flash but if you do not have flash detected with ajax
http://demo.micosolutions.com/afup/ajax-flash-upload-pro-demo/ajax-flash-uploader-demo.html
I think these are the files that can talk about this:
app/design/frontend/base/default/template/catalog/product/view/options/type/file.phtml
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Options/Type/file.php
Edit Deleted old answer because I misunderstood the question.
Ok this time I hope I understood correctly what you want. To get the URL of an custom option image from a product that was added to the cart you can get it from quote item:
$item = $this->getItem();
$optionIds = $item->getOptionByCode('option_ids');
if( $optionIds ) {
$options = array();
foreach( explode( ',', $optionIds->getValue() ) as $optionId ) {
$option = $this->getProduct()->getOptionById( $optionId );
if( $option->getData( 'type' ) == 'file' ) {
$option = $item->getOptionByCode( 'option_' . $optionId );
$value = unserialize( $option->getData( 'value' ) );
var_dump( $value );
var_dump( Mage::getUrl( 'sales/download/downloadCustomOption', array( 'id' => $option->getId(), 'key' => $value[ 'secret_key' ] ) ) );
echo '<img src="' . Mage::getUrl( 'sales/download/downloadCustomOption', array( 'id' => $option->getId(), 'key' => $value[ 'secret_key' ] ) ) . '"/>';
}
}
}
Output: It var dumps download url and $value content ($value[ 'fullpath' ] is the location of the image file but it is forbiddon to access it from a browser - you will have to move the file to a different location in media folder where it will be accessable from the web).
echo will show the image.
$item is of type Mage_Sales_Model_Quote_Item (this code was tested in checkout/cart/index controller - your_page_url/index.php/checkout/cart -> code can be added at the top of template/checkout/cart/item/default.phtml file to see how it works).

Categories