• I have a flexible content template for single-news.php that includes a repeater. How do I add up all the reading time for that post?
    I need to get the count of words for get_sub_field(‘content’)—there may be multiple instances of it (i’ve removed other row layouts for readability). Here is the code that is in the template I am hoping to get a count of.

    if(have_rows('panels')):
    	while(have_rows('panels')) : the_row();
    	if(get_row_layout() == 'content'):
    			$output .= '<section class="row clearfix">';
    			$output .= get_sub_field('content');		
    endif;
    	endwhile;
    
    echo $output;
    endif;

    Any ideas? I know there is a way to use this filter, but i can’t quite figure it out:
    add_filter( 'rtwp_filter_wordcount', 'up_the_count' );

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jason Yingling

    (@yingling017)

    You would use the filter to run a function that gets your field, counts the words, and adds that to the word count.

    function up_the_count( $count ) {
        // Gets the word count of your sub field named 'content'
        $field_count = count( preg_split( '/\s+/', get_sub_field( 'content' ) );
    
        $count += $field_count;
    
        return $count;
    }
    
    add_filter( 'rtwp_filter_wordcount', 'up_the_count' );

    You’d want to make sure you only run that when the page is using that particular template. You could do that with a conditional check.

    And just wanted to note, I didn’t fully test that code, just wrote it here for the comment. Let me know if you have any issues.

    Thread Starter bcook3

    (@bcook3)

    It is still just returning ‘1’. Could it be an issue that it isn’t looping through all instances of the ‘content’ subfield? for any given post we might have 5 content modules, and I think this is just taking just one into consideration?

    Plugin Author Jason Yingling

    (@yingling017)

    Ah yes, in that case you’d probably need to wrap the line $field_count = count( preg_split( '/\s+/', get_sub_field( 'content' ) ); and $count += $field_count; in the repeater looping functionality. That should loop through and get all fields. You may need to make sure you’re passing the proper post id as well as a parameter to the get_sub_field if it isn’t returning your data.

    @bcook3 was wondering if you got this working? I am running into the same issue!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding to $count with ACF flexible content’ is closed to new replies.