This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Image in search results

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #22301
    sabbellasabbella
    Participant

    Hello,

    I’m using ajax search pro for searching in users metadata. When I get the results, the default image next to the title of each search result is the avatar.

    I would like to change the avatar and display a custom image that each user can upload to their profile (meta key name is: foto)

    I tried to display it in “General Options ->Image Options”, but the avatars continues appearing.

    Could you help me to display the “foto” instead of the avatar?

    Thank you,

    #22310
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    This should be possible, but only by using a custom code. Try adding this custom code to the functions.php in your theme/child theme directory (copy from line 3 only!). Before editing, please make sure to have a full site back-up just in case!
    Change the $field variable to the user meta field name, it should do the trick:

    add_filter( 'asp_results', 'asp_get_custom_cf_image', 10, 1 );
    function asp_get_custom_cf_image( $results ) {
    	$field = 'user-image-url'; // Enter the user meta field containing the image
    	foreach ($results as $k=>&$r) {
    		if ( $r->content_type == 'user' ) {
    			$img = get_user_meta($r->id, $field, true);
    			if ( !is_wp_error($img) && !empty($img) )
    				$r->image = $img;
    		}
    	}
    	return $results;
    }

    If the image URL is stored within that field, then this should work.

    #22322
    sabbellasabbella
    Participant

    Thank you very much, Ernest, but it doesn’t work to me, I don’t know why!
    I’ve got something like this in my users directory page to get the images:

    foreach($theusers as $auser)
    {
    $auser = get_userdata($auser->ID); //At the top of the directory.php
    $auser->membership_level = pmpro_getMembershipLevelForUser($auser->ID);
    …..

    if (get_user_meta($auser->ID, ‘foto’, true)) {
    if (in_array( ‘pmpro_role_2’, (array) $auser->roles))

    {

    echo '<a target="_blank" class="'.$avatar_align.'" href="'. add_query_arg('pu', $auser->user_nicename, $profile_url).'">'. (wp_get_attachment_image( $attachment_id, 'thumb')).'</a>';
    

    }

    P.S. I’m displaying the results in the Page results search (Redirect to: Results page)

    Thank you,

    • This reply was modified 7 years, 1 month ago by sabbellasabbella.
    • This reply was modified 7 years, 1 month ago by sabbellasabbella.
    #22327
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    That is the problem then probably. The plugin cannot affect the layout of the results page unfortunately. It is controlled by the search.php file in your currently active theme/child-theme directory. The image parsing must be corrected within that scope.

    #22334
    sabbellasabbella
    Participant

    Ok, thank you very much Ernest 🙂

    #22335
    sabbellasabbella
    Participant

    P.S. just a question; why the title and url can be modified if I’m using the search results page, but the users img can’t be canged?

    #22339
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Because the title, content and some other fields are printed after ‘pre_get_posts’ filters, without actually re-quering these fields. The image requested later on, when it’s printed. There might be a hook available for that, but it’s definitely executed after the search process is fully finished, I am not that familiar with that to be honest.

    #22342
    sabbellasabbella
    Participant

    Thank you very much for the explanation!

    #22354
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Image in search results’ is closed to new replies.