0

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-title"><a 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?

5
  • Are you sure the function is not echoing anything? Have you checked the raw HTML source, in case the content is probably just not visible on screen, e.g. hidden via CSS... But if the function really echo nothing (or an empty string), then you can try deactivating plugins and see if the same issue persists.
    – Sally CJ
    May 13 at 13:07
  • You could also try echo get_the_content() instead of the_content().. to confirm the latter is the one not echoing the content - and if so, try searching for add_filter( 'the_content', add_filter('the_content', etc. in your theme functions file.
    – Sally CJ
    May 13 at 13:23
  • and what is in the none template? Are you sure it's finding posts? If it isn't displaying anything what HTML is it showing instead? Do you mean that the template displays but the_content specifically is blank? Normally if the_content displays nothing it's because either the content is blank, or, because you've tried using the_content filter incorrectly elsewhere
    – Tom J Nowell
    May 13 at 13:26
  • @SallyCJ I have found the issue. I wrote a filter to find iframes in the_content and wrap them in an iframe-container div. This code seems to be conflicting with the output in the search results. I have added it to my original question for reference. May 13 at 13:32
  • Yes, it's indeed conflicting - your function is only returning the content when inside the if block. So there should always be a return line outside the if block. (Or move the return to outside that block)
    – Sally CJ
    May 13 at 15:30

1 Answer 1

1

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
}
1
  • 1
    Thanks for the clarification. I made the adjustments and the code is working as expected now. May 13 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.