• Hi I am using Ajax Search Pro and currently the search results excerpt pulls from content and is one long sentence. I wanted to have the search results excerpt return a list like this:

    Phone Number:
    Serving Area:
    Email:

    The above information (phone number, serving area, and email) currently exist in content, taxonomies and custom fields. I added to taxonomies and custom fields in the process of testing ways to parse the data.

    Ajax Search Pro uses a result template, code included below.

    ?>
    <div class='item asp_result_<?php echo $r->content_type; ?>'>
    
        <?php do_action('asp_res_vertical_begin_item'); ?>
    
        <div class='content'>
    
            <?php if (!empty($r->image)): ?>
    
                <?php do_action('asp_res_vertical_before_image'); ?>
    
                <div class='image'>
                    <img src='<?php echo $r->image; ?>'>
                    <div class='void'></div>
                </div>
    
                <?php do_action('asp_res_vertical_after_image'); ?>
    
            <?php endif; ?>
    
           <h3><a href='<?php echo $r->link; ?>'<?php echo ($s_options['results_click_blank'])?" target='_blank'":""; ?>>
                    <?php echo $r->title; ?>
                    <?php if ($s_options['resultareaclickable'] == 1): ?>
                    <span class='overlap'></span>
                    <?php endif; ?>
            </a></h3>
    
            <?php if ( !empty($r->date) || !empty($r->author) ): ?>
    
            <div class='etc'>
    
                <?php if ( $s_options['showauthor'] == 1 && !empty($r->author) ): ?>
                <span class='author'><?php echo $r->author; ?></span>
                <?php endif; ?>
    
                <?php if ( $s_options['showdate'] == 1 && !empty($r->date) ): ?>
                <span class='date'><?php echo $r->date; ?></span>
                <?php endif; ?>
    
            </div>
    
            <?php endif; ?>
    
            <?php if ($s_options['showdescription'] == 1): ?>
            <!--<p class='desc'>-->
                <?php echo $r->content; ?>
            <!--</p>-->
            <?php endif; ?>
    
        </div>
    
        <?php do_action('asp_res_vertical_after_content'); ?>
    
        <div class='clear'></div>
    
        <?php do_action('asp_res_vertical_end_item'); ?>
    
    </div>
    <div class="asp_spacer"></div>

    The plug-in developer provided the following information:

    Printing post meta into the content

    Since you have access to the post ID via the $r->id, you can get custom field values and put them before/after the content easily.

    If you look ate lines 76-80 in vertical.php, you see the content is being printed there:

    <?php if ($s_options['showdescription'] == 1): ?>
    
        <?php echo $r->content; ?>
    
    <?php endif; ?>

    Let’s get and print a meta value there in a custom div element and modify the code like this:

    <?php
    // This is the meta key
    $key = 'my_meta_key';
    
    // We only want to do this on posts/pages/custom post types
    if ($r->content_type == 'pagepost') {
      $meta_value = get_post_meta( $r->id, $key, true );
      if ($meta_value != '')
        echo "<div class='my_class'>" . $meta_value . "</div>";
    }
    ?>
    
    <?php if ($s_options['showdescription'] == 1): ?>
    
        <?php echo $r->content; ?>
    
    <?php endif; ?>

    If the post meta with the ‘my_meta_key’ exists, then it will be printed before the description, inside a new div element.

    I edited and added the above code to this:

    <?php
    // This is the meta key
    $key = 'phonenumber';
    
    // We only want to do this on posts/pages/custom post types
    if ($r->content_type == 'pagepost') {
      $meta_value = get_post_meta( $r->id, $key, true );
      if ($meta_value != '')
        echo "<div class='my_class'>" . $meta_value . "</div>";
    }
    ?>

    This code only returns “array” in the search results excerpt. Can someone please point me in the right direction? I do not know if it is best to parse the taxonomies or the custom fields or if there is a way to display the content in the excerpt as a list. Any advice is greatly appreciated!

Viewing 1 replies (of 1 total)
  • Your problem is that function get_post_meta is returning a more complex structure than you expect. Please use a debug function to show what is happening, then you will be able to fix the code. Run this code:

    if ($r->content_type == 'pagepost') {
      $meta_value = get_post_meta( $r->id, $key, true );
      if ($meta_value != '') {
        echo "<div class='my_class'>";
        print_r( $meta_value );
        echo "</div>";
      }
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Adding Custom Fields to search results excerpt’ is closed to new replies.