0

I'm building a plugin to create page sections (with ACF and repeater field) and I want to add the sections at the end of the content.

The sections are added to the content, but only when there's something in the editor.

Have you an idea why it doesn't work when there's no content in the editor?

function page_sections( $content ) {

    // check if the repeater field has rows of data
    if ( is_page() && have_rows( 'sections' ) ) :

        $i = 1;

        ob_start();

        // loop through the rows of data
        while ( have_rows( 'sections' ) ) : the_row();
            include( 'partials/page-sections-public-display.php' );
            $i++;

        endwhile;

        $content .= ob_get_clean();

    endif;

    return $content;
}

add_filter( 'the_content', 'page_sections' );

page-sections-public-display.php

<section id="page-section-<?php echo $i; ?>" class="page-section">

    <h2 class="section-title"><?php echo get_sub_field('title'); ?></h2>

    <div class="section-content">
        <?php the_sub_field('content'); ?>
    </div>

</section>
4
  • Does it go inside the have_rows( 'sections' ) condition?
    – Sumit
    May 9, 2016 at 16:39
  • Yes, when the editor is not empty. When the editor is empty, I see no content in the page, event if I return a string instead of the variable $content.
    – benoit
    May 9, 2016 at 17:11
  • It does not happen usually. Please share the code in your template which display the post content.
    – Sumit
    May 9, 2016 at 17:42
  • Thanks, I'll give you more details tomorrow, the theme is maybe the culprit.
    – benoit
    May 9, 2016 at 18:22

1 Answer 1

0

As suggested by @Sumit, the problem was in the template: there was a test that prevented the_content() to execute when the content was empty.

<?php if ( $page->post_content && have_posts() ) while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
<?php endwhile ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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