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
Related
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;
}
I want to display different design template for my shop Music categories and it's all sub categories compare to other categories design template.
For this i have added following if condition:
if (is_product_category( 'music' ))
{
wc_get_template( 'archive-product-cubicles.php' );
} else
{
wc_get_template( 'archive-product.php' );
}
But above code only display new template for Music parent category not it's child categories.
So any one know solutions for this then please inform me.
Thanks,
Ketan.
You can use following:
if (is_product_category( 'music' ) || cat_is_ancestor_of( MUSIC_ID, get_queried_object()->term_id)
{
wc_get_template( 'archive-product-cubicles.php' );
} else
{
wc_get_template( 'archive-product.php' );
}
Replace MUSIC_ID with your own id of music category.
On my Wordpress website, using WooCommerce, on the Checkout page, I am seeing the photos appear above the list of products at Checkout. How do I make them appear IN the list of products, as shown?
This is what I have at the moment:
add_action('woocommerce_checkout_before_order_review', 'displays_cart_products_feature_image');
function displays_cart_products_feature_image() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$item = $cart_item['data'];
if(!empty($item)){
$product = new WC_product($item->id);
// $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->ID ), 'single-post-thumbnail' );
echo $product->get_image();
// to display only the first product image uncomment the line bellow
// break;
}
}
}
This is related to Get Cart products id on checkout WooCommerce page, to display product images.
Thanks!
Instead of woocommerce_checkout_before_order_review, we can use woocommerce_cart_item_name filter like this:
add_action('woocommerce_cart_item_name', 'displays_cart_products_feature_image', 10, 2 );
function displays_cart_products_feature_image( $cart_item_name, $cart_item ) {
return ( is_checkout() ) ? $cart_item['data']->get_image() . $cart_item_name : $cart_item_name;
}
what this will do is put the image before the product name. Something like this image below:
Still not what we need right? Yes but it's the closest thing we can get. And just some CSS will fixed the problem.
.woocommerce-checkout-review-order-table .cart_item .product-name img {
float: right;
}
Will look like this below:
I have set a custom page 'myblog' as 'Posts page' in 'Reading Settings', but why I always get the first post article of 'myblog' instead of 'myblog' itself?
var_dump(get_permalink()); // "http://xxxx.com/myblog/hello-world/"
It should be:
"http://xxxx.com/myblog/"
Any idea why and how I can fix this?
You can do something like this to get the permalink for your blog page (page_for_posts). Below has a couple conditionals checks for fallback, but really the call get_permalink() on the page_for_posts option is what you need.
function get_my_blogpage_permalink() {
if( 'page' == get_option( 'show_on_front' ) ) {
return get_permalink( get_option('page_for_posts' ) );
} else {
return home_url();
}
}
var_dump(get_my_blogpage_permalink());
I have a WooCommerce store and I don't want to display the SKU on any single product page. Looking at their code, I found this filter:
/**
* Returns whether or not SKUS are enabled.
* #return bool
*/
function wc_product_sku_enabled() {
return apply_filters( 'wc_product_sku_enabled', true );
}
and I attempted to override it with this line of code I placed in a custom plugin:
apply_filters( 'wc_product_sku_enabled', false );
I also tried placing the apply_filter inside an action function for woocommerce_product_meta_start which fires right before but it still renders the SKU on the product page. Any ideas?
I think you shoul try with this:
add_filter( 'wc_product_sku_enabled', '__return_false' );
That will remove sku from all woo, back and front end. You can always hide it just by CSS if need it on admin.
The easiest way is with CSS:
.sku_wrapper {
display:none;
}
A more robust approach is to recreate the woocommerce template woocommerce/templates/single-product/meta.php in your own theme and simply comment out the line:
<span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>.</span>
To recreate a woocommerce template in your own theme, see:
http://docs.woothemes.com/document/template-structure/
Hiding the SKU/UGS by using cSS is not an efficient solution because it will be still part of the HTML code.
In order to hide it from the product single page and keep it in the admin page, you have to add this code in the child (or parent if you don’t have the child) functions.php :
// Remove the Product SKU from Product Single Page
add_filter( 'wc_product_sku_enabled', 'woocustomizer_remove_product_sku' );
function woocustomizer_remove_product_sku( $sku ) {
// Remove only if NOT admin and is product single page
if ( ! is_admin() && is_product() ) {
return false;
}
return $sku;
}
Make sure also in the product php page (it can have a different name depending on the theme you use) to have this condition to show the SKU in the product single page:
if (wc_product_sku_enabled() && $product->get_sku()) { // HTML code that shows the SKU in the product single page}
Make Sure to remove it from the frontend only by using this code on function.php usually you can edit the function file on theme editor
add_filter( 'wc_product_sku_enabled', 'my_remove_sku', 10 );
function my_remove_sku( $return, $product ) {
if ( !is_admin() && is_product() ) {
return false;
} else {
return true;
}
}
If you don’t need to use SKUs at all in your shop, you can disable them completely by using this plugin. simply install this plugin. https://wordpress.org/plugins/woocommerce-remove-sku/