Wordpress - changing attributes of featured images - image

I want to use Featured Images (thumbnails) in my posts.
The thing is, I want to be able to change the following image attributes after the image has been attached to the post, but before the post has been published:
Title
Alternate Text
Caption
Description
How do you do that?

<?php
$size = array(150,150);
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( wp_postmeta->_wp_attachment_image_alt )),
'title' => trim(strip_tags( $attachment->post_title )),
);
the_post_thumbnail( $size, $attr );
?>

I'm still not clear what you're trying to do.
This will display the featured image in your markup using the post title as alt and title attributes.
$image_meta = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium');
// replace 'medium' with 'thumbnail', 'large', or 'full'.
echo '<img src="'.$image_meta[0].'" alt="'.$post->post_title.'" title="'.$post->post_title.'" width="'.$image_meta[1].'" height="'.$image_meta[2].'"/>';
If you want to actually change the featured image title, alt, caption, description etc in the database, then you could look at the post_publish hook. This should get you started:
function do_stuff($post_ID){
global $post;
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id){
// Do Stuff with your featured image id - $post_thumbnail_id
}
return $post_ID;
}
add_action('publish_post', 'do_stuff');

try this and its work fine.
$title_attribute = the_title_attribute( array( 'echo' => FALSE ) );
the_post_thumbnail(
'full',
array(
'alt' => $title_attribute,
'title' => $title_attribute
)
);

Related

Custom taxonomy image for a single featured image product

I need to change all featured image products with the images of the term of taxonomy.
I tried this code and I have the right image taxonomy on a single page but does not replace the featured image of the product.
The name of my taxonomy is "settore"
Can you help me, please?
Have a great day
add_action("woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'settore',
) );
}

Get sku and product image in woocommerce emails

I made a copy of the file "email-order-details.php" to my child theme. Now I can modify the html, but would like to add an extra column to my table for the product image. With this code I am able to display the image and sku, but it's in the same column with product name, sku, product description.
<?php
echo wc_get_email_order_items( $order, array( // WPCS: XSS ok.
'show_sku' => $true,
'show_image' => false,
'image_size' => array( 32, 32 ),
'plain_text' => $plain_text,
'sent_to_admin' => $sent_to_admin,
) );
?>
I also want to display the sku without the () and I want to place it on top of the column. I think there must be an array or variables or something like this to get the dates.
This line, for example displays the Price, can anybody tell me how to display sku and product image in the same way?
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
Thank you in advance
try this snippet code, I use this in functions.php in child theme:
function sww_add_wc_order_email_images( $table, $order ) {
ob_start();
$template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, array(
'order' => $order,
'items' => $order->get_items(),
'show_download_links' => $show_download_links,
'show_sku' => $show_sku,
'show_purchase_note' => true,
'show_image' => true,
'image_size' => array( 200, 200 )
) );
return ob_get_clean();
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_wc_order_email_images', 10, 2 );

Woocommerce Filter Product by Attribute

I've been searching for many blogs and forum but this seems not yet answered yet. So i am trying to find a way how to filter product by attribute. I am using ajax to pull data and append it.The question is what will be the query to make a loop depend on what attribute.
Example.
Product A has Color:Blue,Red,Green and has a brand : BrandA , BrandB Product B has Color:Pink,Red,black.
All i want is to get all product with an attribute of Color [red and green] and Brand [BrandA] in a single query without using any plugin.
Here my code in my functions.php
function advanced_search(){
$html = "";
$args = array( 'post_type' => 'product','product_cat' => 'yarn');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ){
$loop->the_post();
ob_start();
get_template_part( 'templates/part', 'search-result' );
$html = ob_get_contents();
ob_end_clean();
}
wp_send_json(array( "product_id" => $product_id , "html" => $html ));
}
add_action('wp_ajax_get_advanced_search', 'advanced_search');
add_action('wp_ajax_nopriv_get_advanced_search', 'advanced_search');
I dont know where should i put the product attributes in my query. I hope anyone found an answer for this.Thank you very much.
You should be able to achieve what you're looking to do by using a tax_query combined with your WP_Query. Attached is a small example using a 'Brand' attribute and the 'EVGA' term inside it (which WooCommerce will turn into 'pa_brand' - product attribute brand).
$query = new WP_Query( array(
'tax_query' => array(
'relation'=>'AND',
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => 'evga'
)
)
) );
You can find some more documentation on the above links to string a few tax queries together for additional filtering and functionality.

Display a random post attachment on homepage in Wordpress

I'm trying to get 1 random image attached to a post, and display it on the front page (changing which image is displayed on refresh). All the code I've seen is showing how to display an attachment from within the loop on the post page, but this will be getting the attachment from one page and displaying it on a different page.
Any help would be huge as I don't really have a starting point on this.
You can use WP_Query to query attachments directly. They is actually a post type just for attachments. This bit of code will spit out the <img> tag for a random image:
$query = new WP_Query( array( 'post_status' => 'any', 'post_type' => 'attachment' ) );
$key = array_rand($query->posts, 1);
echo wp_get_attachment_image($query->posts[$key]->ID, 'medium');
The string medium can be replaced with another size you set in the media section of the dashboard, or a custom size you set in the code using add_image_size().
Untested, but this should grab only one row from your database, and it makes full use of the WP_Query functions.
You can change the size of the image that is displayed by replacing 'full' with an array (e.g. array(200, 130)). Check the codex for wp_get_attachment_image() for more informaion.
$args = array(
'orderby' => 'rand',
'post_type' => 'attachment'
'post_status' => 'inherit',
'posts_per_page' => 1
)
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts() : $query->the_post();
echo wp_get_attachment_image(get_the_ID(), 'full');
endwhile;
wp_reset_postdata();
endif;

How to get and set image attachment metadata in wordpress?

I have post in WP. This post has attached image as attachment. I set description of this image via dialog (gallery tab) in post edit section.
Now I need WP functions to programmatically get all metadata for this attachment (description,title,caption,...) and another function to set the same data.
What functions to use ?
use get_children() to retrive atachement for posts.
$args = array(
'numberposts' => -1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children( $args );
a full example here Get URLs, Captions & Titles for Images Attached to Posts in WordPress
This works for me:
<?php
foreach ( $attachments as $attachment_id => $attachment ) {
$src = wp_get_attachment_image_src( $attachment_id, 'full' );
var_dump($src);
} ?>
array
0 => string 'http://example.com/wp-content/uploads/2009/08/DSC00261.JPG' (length=63)
1 => int 1632
2 => int 1224
3 => boolean false
The order of the array is allocated as follows.
$src[0] => url
$src[1] => width
$src[2] => height
$src[3] => icon

Resources