Vendor image only on listing page

I am trying to display as a shortcode only the vendor image to the listing page, but so far it doesn’t seem to work.
Any suggestions what could be wrong with the below code?

function vend_img() {
	
	$user_id = get_current_user_id();
  
  $user = \HivePress\Models\USer::query()->get_by_id($user_id);
	$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $user_id])->get_first(); 
	
	
    $_image = '<a href="<?php echo esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ); ?>" class="hp-form__action hp-form__action--vendor-view hp-link"><i class="hp-icon fas fa-eye"></i><span><?php esc_html_e( 'View Profile', 'hivepress' ); ?><h4 class="hp-vendor__name"><?php echo esc_html( $user->get_display_name() ); ?></h4><div class="hp-vendor__image">
	<?php if ( $vendor->get_image__url( 'hp_square_small' ) ) : ?>
		<img src="<?php echo esc_url( $vendor->get_image__url( 'hp_square_small' ) ); ?>" alt="<?php echo esc_attr( $vendor->get_name() ); ?>" loading="lazy">
	<?php else : ?>
		<img src="<?php echo esc_url( hivepress()->get_url() . '/assets/images/placeholders/user-square.svg' ); ?>" alt="<?php echo esc_attr( $vendor->get_name() ); ?>" loading="lazy">
	<?php endif; ?>
	
    return $_image;




}
 add_shortcode('vend_image', 'vend_img');

Please try this PHP snippet to create the shortcode.

add_action(
	'init',
	function(){
		add_shortcode('vend_image', function() {
			$user_id = get_current_user_id();
  
  			$user = \HivePress\Models\USer::query()->get_by_id($user_id);
			$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $user_id])->get_first(); 
	
	
    		$_image = '<a href="'.esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ).'" class="hp-form__action hp-form__action--vendor-view hp-link"><i class="hp-icon fas fa-eye"></i><span>View Profile<h4 class="hp-vendor__name">'.esc_html( $user->get_display_name() ).'</h4><div class="hp-vendor__image">';
	
			if($vendor->get_image__url( 'hp_square_small' )){
				$_image .= '<img src="'.esc_url( $vendor->get_image__url( 'hp_square_small' ) ).'" alt="'.esc_attr( $vendor->get_name() ).'" loading="lazy">';
			}else{
				$_image .= '<img src="'.esc_url( hivepress()->get_url() . '/assets/images/placeholders/user-square.svg' ).'" alt="'.esc_attr( $vendor->get_name() ).'" loading="lazy">';
			}
	
			$_image .= '</div></a>';
	
    		return $_image;
		}
		);
	},
	1000
);

Then it is possible to add the shortcode [vend_image] to the listing page if you try to edit the listing template in the HivePress/Templates. There is a tutorial on how to use the HivePress/Templates functionality Overview | HivePress Templates - YouTube

Also, it is possible to add the shortcode on the listing page with this code snippet.

add_filter(
	'hivepress/v1/templates/listing_view_page',
	function($template){
		return hivepress()->template->merge_blocks(
			$template,
			[
				'page_content' => [
					'blocks' => [
						'custom_shortocode_listing' => [
							'type' => 'content',
    						'content' => do_shortcode('[vend_image]'),
    						'_order' => 1,
						]
					],
				],
			],
		);
	},
	1000
);

But please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

Hi Yevhen,

Thank you for the code, it seems to be pulling the post author image rather than the vendor image.
For example, I have these fictional vendors that are coming with the rentalhive theme, instead of showing their name and picture it shows my profile.
Basically does the same thing as the post author block in the editor.

I did find a workaround in CSS which seems to work quite well.
Please find it below:

.hp_listing-template-default .hp-vendor–view-block .hp-vendor__footer, .hp-vendor–view-block .hp-vendor__attributes–secondary .hp-col-xs-12, .hp-vendor–view-block .hp-vendor__rating, .hp-vendor–view-block .hp-vendor__verified-badge
{
display: none !important;

}

Please try this PHP snippet instead to create a shortcode. It will only show a vendor profile image.

add_action(
	'init',
	function(){
		add_shortcode('vend_image', function() {
			$user_id = get_current_user_id();
  
  			$user = \HivePress\Models\USer::query()->get_by_id($user_id);
			$vendor = \HivePress\Models\Vendor::query()->filter(['user' => $user_id])->get_first(); 
	
	
    		$_image = '<a href="'.esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ).'" class="hp-form__action hp-form__action--vendor-view hp-link"><div class="hp-vendor__image">';
	
			if($vendor->get_image__url( 'hp_square_small' )){
				$_image .= '<img src="'.esc_url( $vendor->get_image__url( 'hp_square_small' ) ).'" alt="'.esc_attr( $vendor->get_name() ).'" loading="lazy">';
			}else{
				$_image .= '<img src="'.esc_url( hivepress()->get_url() . '/assets/images/placeholders/user-square.svg' ).'" alt="'.esc_attr( $vendor->get_name() ).'" loading="lazy">';
			}
	
			$_image .= '</div></a>';
	
    		return $_image;
		}
		);
	},
	1000
);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.