Adjust title of posts in search results

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 Ernest Marcinko 3 years, 5 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #22083
    custompczone
    custompczone
    Participant

    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,
    Adam

    #22091
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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 :)


    #22106
    custompczone
    custompczone
    Participant
    You cannot access this content.
    #22117
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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 :)


    #22139
    custompczone
    custompczone
    Participant
    You cannot access this content.
    #22141
    custompczone
    custompczone
    Participant
    You cannot access this content.
    #22147
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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.

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #22153
    custompczone
    custompczone
    Participant

    Thank 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

    #22164
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.