• Resolved holisticremedysearch

    (@holisticremedysearch)


    hi there

    i’m having trouble setting my taxonomy term as a variable and truncating it for use in autocomplete.php via any method. has anyone ever done this before ? i was able to output the taxonomy variable, just cant shorten it now

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter holisticremedysearch

    (@holisticremedysearch)

    what PHP file does this go to ?

    after i try above, i can reference it in Automcomplete.php / instantsearch.php ?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Revising my code example slightly,

    function my_trim_overview( array $shared_attributes, \WP_Post $post ) {
    // Fetch $overview_field here.
    $overview_field = 'Some text';
    // Trim to 20 words
    $words = 20;
    // Store the manual trimmed version for usage in the index.
    $shared_attributes['overview_snippet'] = wp_trim_words( $overview_field, $words );

    return $shared_attributes;
    }
    add_filter( 'algolia_searchable_post_shared_attributes', 'my_trim_overview', 10, 2 );
    add_filter( 'algolia_post_shared_attributes', 'my_trim_overview', 10, 2 );

    I’m changing the property to overview_snippet instead of overview in case you decide you want both versions in the object.

    This would go in your active theme’s functions.php or anywhere else that you may have for where action/filter callbacks can be added to.

    Then it should be available on data.overview_snippet in the autocomplete/instantsearch.php template blocks, if I’m remembering my syntax properly.

    Thread Starter holisticremedysearch

    (@holisticremedysearch)

    those dont seem to work, at the root of it, just trying to get data.overview to be assigned as a variable so i can string manipulate it in PHP or JS.

    whenever i do that in autocomplete.php it doesnt read the {{{ part.

    $overview = echo data.overview

    $overview = data.overview

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yes but “data.overview” here would still be your REALLY long value that you’re trying to truncate.

    Going based off of your code screenshot at https://snipboard.io/OHuKmv.jpg

    I would amend the code in your wds_algolia_custom_fields callback function to include my manual version of truncation. For example, add just these two lines, after line 206

    $words = 20;
    $attributes[ $field . '_snippet' ] = wp_trim_words( $data, $words );

    That way, you’d have your data.overview property indexed, and then right after it, the data.overview_snippet property indexed, and then you can an should be able to use data.overview_snippet where needed.

    Thread Starter holisticremedysearch

    (@holisticremedysearch)

    that seemed to work! just seeing a weird encoding issue at the end but working through that now.

    seems like its related to wp_trim_words so i can fix that

    https://snipboard.io/1WbyUf.jpg

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    pass an empty string for the 3rd parameter. That should shortcode the “read more” text that will default to “…” and the encoded version you’re seeing.

    wp_trim_words( $data, $words, '' );
    Thread Starter holisticremedysearch

    (@holisticremedysearch)

    yup, fixed that. that’s all there is for this one

    thanks so much for your help!

Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘Truncate Searchable Attribute in JS, HTML or PHP’ is closed to new replies.