• Resolved missmikado

    (@missmikado)


    Hi,

    I activated the excerpt in the backend on pages with this code in the functions.php:

    add_action( 'init', 'kb_page_excerpts' );
    
    function kb_page_excerpts() {
      add_post_type_support( 'page', 'excerpt' );
    }

    Works fine.
    Now, I tried to achieve the following in the search-results:

    IF there is a given excerpt: show the excerpt
    ELSE show the content, but with the ‘ocean_search_results_excerpt_length’ [30 letters]

    I found the partials/search/content.php with this code in it:

    <?php
    /**
     * Search result page entry content
     *
     * @package OceanWP WordPress theme
     */
    
    // Exit if accessed directly
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    global $post;
    
    // Excerpt length
    $length = apply_filters( 'ocean_search_results_excerpt_length', '30' ); ?>
    
    <div class="search-entry-summary clr"<?php oceanwp_schema_markup( 'entry_content' ); ?>>
        <p>
    	    <?php
    	    // Display custom excerpt
    	    echo wp_trim_words( strip_shortcodes( $post->post_content ), $length ); ?>
        </p>
    </div><!-- .search-entry-summary -->

    I changed post_content to post_excerpt but the result is, when no excerpt is given, nothing is showing up – but I want the auto generated beginning of the post/page instead.

    I managed it, to show the excerpt (if there is one) or the content, but the content was not shortend to 30 letters and it was formatted like on the page (with background-image etc) and not plain text, as it should be on a search result page.

    How do I have to change the code?
    Thank you very much!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Theme Author oceanwp

    (@oceanwp)

    Hello, I added this filter “ocean_search_results_excerpt_length”, so you can change easily the length, you just need to add a function like this in the functions.php file of your child theme:

    function my_search_results_excerpt_length() {
    	return '50';
    }
    add_filter( 'ocean_search_results_excerpt_length', 'my_search_results_excerpt_length' );
    Thread Starter missmikado

    (@missmikado)

    Hello,

    thank you.maybe I didn’t make it completely clear:

    It’s not about the length itself. It’s about the fact, that I DON’T want to show the shortend content, but the excerpt I typed in.
    Just if there ist no excerpt typed in, the shortend content should be shown.

    Theme Author oceanwp

    (@oceanwp)

    Hello, oh, no, the search result page return the post content, not the excerpt.

    Thread Starter missmikado

    (@missmikado)

    Yes, I know – and that‘s why i was asking how i have to change the php code to change it to excerpt (or if not existing take content).
    Is my question so confusing?

    Theme Author oceanwp

    (@oceanwp)

    Ok, add the content.php file in oceanwp/partials/search/ in the exact same path into your child theme, open this file in your text editor and replace:

    <?php
    // Display custom excerpt
    echo wp_trim_words( strip_shortcodes( $post->post_content ), $length ); ?>

    by:

    <?php
    // Display excerpt
    if ( has_excerpt( $post->ID ) ) {
        the_excerpt();
    }
    
    // Display custom excerpt
    else {
        echo wp_trim_words( strip_shortcodes( $post->post_content ), $length );
    } ?>
    Thread Starter missmikado

    (@missmikado)

    Works like a breeze, thank you!
    Problem solved

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Search – show excerpt if / else’ is closed to new replies.