I have created an image upload custom field on all of my Woocommerce product category pages. I have added the following code, which is working, to be able to pull in the image to my category pages through the archive-product.php page:
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load thumbnail for this taxonomy term (term object)
$featuredbackground = get_field('category_image_upload', $taxonomy . '_' . $term_id);
?>
<section class="page-top-image" style="background-image: url('<?php echo $featuredbackground; ?>'); background-size: 100% 100%; height: auto;">
<header>
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
</header>
</section>
This code pulls images associated with a category/taxonomy.
The issue I am having is with the 'shop' page since it is not a category page. I want to pull in the image from the shop base page when the main base shop page is being displayed. I have a custom field for image upload added to the WP WOO Shop Base page. My thought is using an if/else statement to accomplish this but I can't seem to make it work. Here is what I have tried...
<?php if ( class_exists( 'WooCommerce' ) ) { ?>
<?php if ( is_shop() ) { ?>
<?php $featuredbackground = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<section class="page-top-image" style="background-image: url('<?php echo $featuredbackground[0]; ?>'); background-size: 100% 100%; height: auto;">
<header>
<h1 class="page-title"><?php post_type_archive_title(); ?></h1>
</header>
</section>
<?php } ?>
<?php } else { ?>
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load thumbnail for this taxonomy term (term object)
$featuredbackground = get_field('category_image_upload', $taxonomy . '_' . $term_id);
?>
<section class="page-top-image" style="background-image: url('<?php echo $ featuredbackground; ?>'); background-size: 100% 100%; height: auto;">
<header>
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
</header>
</section>
<?php } ?>
Admittedly, I haven't worked with if/else statements in PHP much at all. Learning as I go. Any guidance would be greatly appreciated!
Thanks!
Aucun commentaire:
Enregistrer un commentaire