Search – show excerpt if / else
-
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
topost_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!
- The topic ‘Search – show excerpt if / else’ is closed to new replies.