Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Image in search results
- This topic has 8 replies, 2 voices, and was last updated 7 years, 1 month ago by
Ernest Marcinko.
-
AuthorPosts
-
April 24, 2019 at 3:02 pm #22301
sabbella
ParticipantHello,
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,
April 25, 2019 at 10:14 am #22310Ernest Marcinko
KeymasterHi,
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.
April 25, 2019 at 5:41 pm #22322sabbella
ParticipantThank 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,
April 26, 2019 at 7:40 am #22327Ernest Marcinko
KeymasterHi,
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.
April 26, 2019 at 9:03 am #22334sabbella
ParticipantOk, thank you very much Ernest 🙂
April 26, 2019 at 9:19 am #22335sabbella
ParticipantP.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?
April 26, 2019 at 1:47 pm #22339Ernest Marcinko
KeymasterHi,
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.
April 26, 2019 at 2:51 pm #22342sabbella
ParticipantThank you very much for the explanation!
April 29, 2019 at 7:53 am #22354Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- The topic ‘Image in search results’ is closed to new replies.