Post and page content not displaying in search results

I am using a content-search.php template part to output my search results. The post/page titles and links to those posts/pages are working correctly, but for some reason the the_content() function is not rendering any HTML. I have tested for errors with debugging on, but nothing is displayed.

Here is my content-search.php template code:

article id=post-?php the_ID(); ? ?php post_class(); ?
  header class=entry-header
    ?php the_title( sprintf( 'h2 class=entry-titlea href=%s rel=bookmark', esc_url( get_permalink() ) ), '/a/h2' ); ?
  /header!-- .entry-header --

  div class=entry-content
    // No content from posts or pages is displayed using the_content function
    ?php the_content(); ?
  /div!-- .entry-content --

  footer class=entry-footer
    ?php flagstaffcounty_entry_footer(); ?
  /footer!-- .entry-footer --
/article!-- #post-?php the_ID(); ? --

Here is the search.php template code:

get_header(); ?
div class=content-and-sidebar
  main class=main
    ?php get_search_form(); ?
    ?php if ( have_posts() ) : ?
      ?php while ( have_posts() ) :
        the_post();
        get_template_part( 'template-parts/content', 'search' );
      endwhile;
      the_posts_navigation();
    else :
      get_template_part( 'template-parts/content', 'none' );
    endif;
    ?
  /main!-- main --
  aside class=sidebar
    ?php get_sidebar(); ?
  /aside!-- aside --
/div!-- .content-and-sidebar --
?php get_footer();

The the_content function is working on all other templates on my site.

Update

Including offending iframe wrapper filter function for reference.

function themename_iframe_wrapper($content) {
  if( is_page() ) {
    $pattern = '~iframe.*/iframe~';
    preg_match_all($pattern, $content, $matches);

    foreach ($matches[0] as $match) {
      $wrappedframe = 'div class=iframe-container' . $match .   '/div';
      $content = str_replace($match, $wrappedframe, $content);
    }
    return $content;
  }
}
add_filter('the_content', 'themename_iframe_wrapper');

Any ideas on what I am missing?

Topic the-content templates theme-development Wordpress search

Category Web


The problem in your code is that your function is only returning the content when is_page() is true, which is not on the default search results page, where is_search() is true. Likewise, the content would also not be returned on singular post pages and anywhere where is_page() is not true.

So remember that a filter callback (like your themename_iframe_wrapper() function) must always return the content, just as with shortcode handler functions.

function themename_iframe_wrapper($content) {
  if( is_page() ) {
    ... your code.
    return $content; // don't just return it here
  }

  return $content; // always return the content
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.