I’m working on a WordPress site that shows products in a portfolio grid layout. The theme uses a standard index.php for the loop and handles content output through a functions file based on post type and configuration.
I want to create a hover effect where moving the mouse over the main thumbnail shows a different image stored in a custom field named “hover-thumb”. I’ve tried implementing this but the hover image source appears empty in the HTML output even though the hover functionality seems to work.
I’m not experienced with programming so this is challenging for me. I modified the functions file code by adding PHP inside the overlay span element but something isn’t working correctly. The src attribute stays empty when I inspect the page source.
I’d prefer to add this functionality to my child theme’s functions file instead of modifying the parent theme files, but I’m not sure how much of the original code I need to include or how to properly override just the parts I want to change.
<?php
$thumbnail_src = '';
$img_width = 'on' === $full_width ? 1080 : 400;
$img_width = (int) apply_filters( 'custom_portfolio_img_width', $img_width );
$img_height = 'on' === $full_width ? 9999 : 284;
$img_height = (int) apply_filters( 'custom_portfolio_img_height', $img_height );
$css_class = 'on' === $full_width ? 'main_portfolio_image' : '';
$alt_text = get_the_title();
$featured_img = get_thumbnail( $img_width, $img_height, $css_class, $alt_text, $alt_text, false, 'PostImage' );
$thumbnail_src = $featured_img["thumb"];
if ( '' !== $thumbnail_src ) : ?>
<a href="<?php the_permalink(); ?>">
<?php if ( 'on' !== $full_width ) : ?>
<div class="portfolio_thumbnail">
<?php endif; ?>
<?php display_thumbnail( $thumbnail_src, $featured_img["use_timthumb"], $alt_text, $img_width, $img_height ); ?>
<?php if ( 'on' !== $full_width ) : ?>
<div class="image_overlay"><?php $secondary_image = get_post_meta($post->ID, 'hover-thumb', true); ?>
<img class="overlay-image" src="<?php echo $secondary_image; ?>" /> </div>
</div>
<?php endif; ?>
</a>