I’ve created a field group called ‘main_image’ and under it I have 2 fields
1.
‘main_image_logged’
type: image
2.
‘main_image_logout’
type:image
what im trying to do is to show the classic featured image which comes with the post for all, and for users that is logged in show the image in field “main_image_logged’
for the logout i even tried to set name:_thumbnail_id which took the image from ‘main_image_logout’ and used it as a featured image.
is there any way how to do this?
if user is logged out -> featured image from field ‘main_image_logout’
if user is logged in -> featured image from field ‘main_image_logged’
tried something like this but it's wrong
function acf_set_featured_image( $value, $post_id, $field ){
if (is_user_logged_in()) {
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
}
add_filter('acf/update_value/name=main_image_logged', 'acf_set_featured_image', 10, 3);
using:
Wordpress
Advanced custom fields
Divi theme
thanks alot guys
Try this
add_filter('post_thumbnail_id', 'replace_thumbnail_id_with_acf', 20, 2);
function replace_thumbnail_id_with_acf($thumbnail_id, $post) {
if ( is_user_logged_in() ) {
$image_id = get_field('reveal_face', $post->ID, false);
if ($image_id) {
$thumbnail_id = $image_id;
}
} else {
$image_id = get_field('_thumbnail_id', $post->ID, false);
if ($image_id) {
$thumbnail_id = $image_id;
}
}
return $thumbnail_id;
}
How can I assign multiple images to a product in WooCommerce?
I've tried:
update_post_meta( $post_id, '_product_image_gallery', $image_id);
but it assigns only one image. When I use an array, it does not work:
update_post_meta( $post_id, '_product_image_gallery', array($image_id,$image_id2));
If you have multiple images that need to be assigned to a product, you will need to assign one image as the featured image/thumbnail, and then assign the rest of the images as the product gallery thumbnails.
Below is a quick function that can achieve this for you:
function so_upload_all_images_to_product($product_id, $image_id_array) {
//take the first image in the array and set that as the featured image
set_post_thumbnail($product_id, $image_id_array[0]);
//if there is more than 1 image - add the rest to product gallery
if(sizeof($image_id_array) > 1) {
array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
}
}
Assuming you have an array of image id's, and the id of the product that you want to attach the images to, you can call the function above like this:
$images = array(542, 547, 600, 605); //array of image id's
so_upload_all_images_to_product($product_id, $images);
If you are working with a massive array of product images, or you are very serious about micro-optimisation, you can use a combination of array_reverse and array_pop instead of array_shift.
Try like this:
update_post_meta( $post_id, '_product_image_gallery', $image_id.",". $image_id2);
Example to complete:
$images_ids = array();
$images_meta = "";
foreach ($images_ids as $im_id) {
if (is_null(get_post_meta($post_id,"_product_image_gallery")))
add_post_meta($post_id,"_product_image_gallery",$im_id);
else {
$images_meta = get_post_meta($post_id,"_product_image_gallery",true);
update_post_meta($post_id,"_product_image_gallery",$images_meta.",".$im_id);
}
Here is a little wrapper for this job. It accepts only one attachment and adds it either as the base image or adds to the gallery if the product has a base image already.
/**
* #param $product WC_Product|WP_Post
* #param $attachmentId int
*/
function setOrAddImageToProduct($product, $attachmentId)
{
if(!has_post_thumbnail($product->ID)) {
set_post_thumbnail( $product, $attachmentId );
} else {
$gallery = get_post_meta($product->ID, '_product_image_gallery');
if(!empty($gallery)) {
$galleryItems = explode(",", $gallery);
$galleryItems[] = $attachmentId;
} else {
$galleryItems = [$attachmentId];
}
update_post_meta($product->ID, '_product_image_gallery', join(',', $galleryItems));
}
//Adds connection to the product for the media view
$attachment = get_post( $attachmentId );
$attachment->post_parent = $product->ID;
wp_update_post( $attachment );
}
This code will upload multiple images in product gallery
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_file_upload" => $file);
$newupload = my_handle_attachment( "my_file_upload", $post_id);
}
$attach_newupload_ids[]=$newupload;
}
$attach_ids=implode(',',$attach_newupload_ids);
if($attach_ids){
update_post_meta( $post_id, '_product_image_gallery', $attach_ids);
}
This code will update the product gallery when a new image is loaded to the product. I', using it in a React app that uses wp as back-end, the new image is loaded trough the app and then the hook "add action" triggers the code that includes the image into the product gallery.
I use this code in the functions.php file:
<?php
add_action( 'add_attachment', 'auto_on_upload' );
function auto_on_upload( $attachment_id ) {
$parent = get_post_ancestors( $attachment_id );
$images = get_attached_media('', $parent[0]);
foreach( $images as $key => $image) {
$images_array[] = $key;
};
$images_array[] = $attachment_id;
update_post_meta($parent[0], '_product_image_gallery', implode(',', $images_array));
}
?>
I want to set a default 'Featured Image' for every category that I have.
In my functions.php file I have the following code:-
/* ---------------FEATURED POST IMAGE------------------------*/
function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment);
}}
else if ( in_category('2') ) {
set_post_thumbnail($post->ID, '112');
}
else if ( in_category('3') ) {
set_post_thumbnail($post->ID, '115');
}
else if ( in_category('4') ) {
set_post_thumbnail($post->ID, '113');
}
else if ( in_category('8') ) {
set_post_thumbnail($post->ID, '114');
}
else {
set_post_thumbnail($post->ID, '0');
}
}
}
add_action('the_post', 'default_category_featured_image');
Which is working for most of the Categories apart from Wordpress. There are 6 posts at the minute that have 'Wordpress' as the category and they all are using category ID '3', but for some unknown reason, a few of the posts that have 'Wordpress' as the category have the default featured image set, where as a few don't have any default image set?
Any idea why this is happening:-
You can see the problem here:-
http://www.web-tricks.co.uk
From the homepage you can see the title 'How to increase WordPress Memory Limit' is working, and 'Web-Tricks Top 10 Best Plugins for WordPress' is now, but they have the same category ID - Any ideas?
You could create a new template for each category, and do a WP_Query that assigns the category.
Then within the loop, you can say if have featured image, show, otherwise show my image.
If that makes sense. If not, i'll try post an example soon. I'm on the fly atm :P
When i create a page, add a gallery, and browse this gallery on the front-end it will browse all the attachments that are associated with that page, instead of just the images within that gallery. Is there a way to filter all the other attachments and only show the images within a certain gallery? So that, for instance, when I delete the gallery and add a new gallery on the same page > only the new gallery is shown?
Any ideas?
This might not be the most elegant way, but i've found it very usefull.
Passing a post ID to the function below will load a gallery from the post_content of that post. So you would create a gallery and insert it into your post content, then in the template you run this function and will be returned with an array of attachments in that gallery which you are free to to whatever with, i.e slideshows and the likes.
function wp_load_gallery($post_id) {
$post = get_post( $post_id );
$regx = '/' . get_shortcode_regex() . '/';
preg_match( $regx, $post->post_content, $matches );
$ids = shortcode_parse_atts( $matches[3] );
$gallery = array( );
foreach( explode( ',', $ids['ids'] ) as $id ) {
if($id) {
$gallery[] = get_post( $id );
}
}
return $gallery;
}
Note that the shortcode is not cut from the content, so when you display the content you should run it through the strip_shortcodes function, i.e:
echo strip_shortcodes( get_the_content() );
This allows you to update the gallery whenever you want with whatever you want.
EDIT:
To simply display all images:
$gallery = wp_load_gallery($YOUR_POST_ID);
foreach($gallery as $image) {
echo wp_get_attachment_image($image->ID);
}
In Wordpress.. I'm looking for a way to add the first uploaded image in a post automatically to a custom field named "Image".
Does anyone know this?
I found the answer my self
Here is the final code that adds the thumbnail url to a custom field named Image.
function w_thumbnail_src() {
if (has_post_thumbnail()) {
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
return $thumb[0]; // thumbnail url
} else {
return ''; // or a default thumbnail url
}
}
add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
global $wpdb;
if(!wp_is_post_revision($post_id)) {
add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
}
}