Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Adjust title of posts in search results
This topic contains 8 replies, has 2 voices, and was last updated by Ernest Marcinko 4 years, 7 months ago.
- AuthorPosts
- April 9, 2019 at 2:42 pm #22083
Greetings,
I have a question about editing the title of search results.
Currently, I’m using Advanced custom fields to change how the title of posts appear. Using the code below, an <h2> tag is added to the title of each post that is listed based on the array value.
<?php $trade_status = get_field('trade_status'); ?> <?php if( in_array('30', $trade_status) ): ?> <h2 class="new">New</h2> <?php endif; ?> <?php if( in_array('20', $trade_status) ): ?> <h2 class="active">Active</h2> <?php endif; ?> <?php if( in_array('10', $trade_status) ): ?> <h2 class="closed">Closed</h2> <?php endif; ?> </div>
Is there any way I can make ajax pro do the same? That is, customize the titles of the posts being displayed based on an ACF value. I believe similar code would be applicable for this if it’s possible.
Thanks a lot,
AdamApril 10, 2019 at 8:44 am #22091Hi,
Yes, that is possible as well via a custom code. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); function asp_custom_field_to_results( $results ) { $custom_field = 'trade_status'; foreach ($results as $k=>&$r) { if ($r->content_type != 'pagepost') continue; if ( function_exists('get_field') ) $trade_status = get_field( $r->id, $custom_field, true ); // ACF support else $trade_status = get_post_meta( $r->id, $custom_field, true ); // Modify the post title to add the meta value if ( !empty($trade_status) ) { if ( in_array('30', $trade_status) ) { $html = '<h2 class="new">New</h2>'; } else if ( in_array('20', $trade_status) ) { $html = '<h2 class="active">Active</h2>'; } else if ( in_array('10', $trade_status) ) { $html = '<h2 class="closed">Closed</h2>'; } else { $html = ''; } $r->title .= $html; } } return $results; }
I did not test this code, but it should be very close to a possible solution.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
April 10, 2019 at 9:18 pm #22106You cannot access this content.April 11, 2019 at 9:02 am #22117Hi,
Okay, that is not going to work that way. Instead of that plugin, I would suggest maybe this one instead. This does not require file editing, as you can add the custom code snippets via the plugin back-end. That should very likely do the trick.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
April 12, 2019 at 8:59 pm #22139You cannot access this content.April 12, 2019 at 9:34 pm #22141You cannot access this content.April 15, 2019 at 8:41 am #22147Hi,
Thank you, I think there was a minor error within the code, and I also changed the output to display the custom HTML before the title. Please try this one instead.
Best,add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); function asp_custom_field_to_results( $results ) { $custom_field = 'trade_status'; foreach ($results as $k=>&$r) { if ($r->content_type != 'pagepost') continue; if ( function_exists('get_field') ) $trade_status = get_field( $custom_field, $r->id, true ); // ACF support else $trade_status = get_post_meta( $r->id, $custom_field, true ); // Modify the post title to add the meta value if ( !empty($trade_status) ) { if ( in_array('30', $trade_status) ) { $html = '<h2 class="new">New</h2>'; } else if ( in_array('20', $trade_status) ) { $html = '<h2 class="active">Active</h2>'; } else if ( in_array('10', $trade_status) ) { $html = '<h2 class="closed">Closed</h2>'; } else { $html = ''; } $r->title = $html . $r->title; } } return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
April 15, 2019 at 7:04 pm #22153Thank you x1,000,000 it works perfectly.
You are the bomb. I really appreciate you taking the time to sort this out with me. You truly went above and beyond. OMW to write an A+ review now (if I haven’t already).
Have a great week.
Adam
April 16, 2019 at 8:31 am #22164You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.