Site logo

Archived topic

Can I bring custom dynamic content from posts into a query loop?

16 replies · Started by Michelle on December 11, 2022

Viewing posts 1–15 of 17

I am using php to add an estimated reading time to my single post meta. I want to add it to my blog page as well so people can see at a glance how long an article will take to read. Problem is, the estimator on the blog page only considers the excerpt length, not the entire article.

How can I display the estimated time from my Single Posts on my blog page?

I *could* create an ACF, add it into Elements, create a place with a Headline block where I could manually add the time in the post editor directly after publishing the post and seeing what the estimated reading time says on the single post page, but that seems like a "fix it with tape" kind of solution.

Thanks. Yes, here is my Blog page. (It's under maintenance, so you'll have to log in.) My credentials are below.

That's odd. Your code should take into account the entire article.

Can you try this code instead to test?:

function tu_estimated_reading_time() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = str_word_count( $clean_content );
	$m = floor($word_count / 200);
    $s = floor($word_count % $wpm / ($wpm / 60));
	if($m < 1) {
		$time = $s . ' second' . ($s <= 1 ? '' : 's');
	} else {
		$time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's');
	}
	
    return $time;
}

add_filter( 'generate_post_author_output', function( $output ) {
    $output .= '<span class="read-time">Reading time: ' . tu_estimated_reading_time() . '</span>';

    return $output;
} );

Let us know how it goes.

I put the new code in and found that it doesn't pull up the time; It just leaves the shortcode [reading-time]...

Oh. I see what's the issue. Sorry, I didn't notice you're using a static page for your Blog Page.

You need to add the estimated time differently to make it work.

First, instead of a Shortcode Block, add a GB Headline Block instead.

Give it a text of Placeholder and a class cu-estimated-time.

Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

Then, add this Snippet:

function tu_estimated_reading_timeb() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = str_word_count( $clean_content );
	$m = floor($word_count / 200);
    $s = floor($word_count % $wpm / ($wpm / 60));
	if($m < 1) {
		$time = $s . ' second' . ($s <= 1 ? '' : 's');
	} else {
		$time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's');
	}
	
    return $time;
}

add_filter( 'render_block', function( $block_content, $block ) {
    if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-estimated-time' ) !== false ) {
		$myreplace1 = 'Placeholder';
		$myinsert1 = tu_estimated_reading_timeb();
        $block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
    }

    return $block_content;
}, 10, 2 );

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Let us know how it goes.

Hello. I added the text and class, plus the code snippet and received this message:

Don't Panic
The code snippet you are trying to save produced a fatal error on line 73:

is_readable(): open_basedir restriction in effect. File(/dev/urandom) is not within the allowed path(s)

That's odd. Can I try to add the code myself to your site so I can see the error firsthand?

Yes, go ahead.

That worked! Now all that's left is making the single post page estimated times match the time stated on the blog page.

Which format do you prefer - the one currently on the Blog Page or the one on the Single Post Pages?

I like the format on the single post pages the best - "4 min read". Most important though is that the estimated times agree. My current post says 4 minutes and 52 seconds on the blog page, and 4 minutes on the single post page.

I see. Replace the code I added in Code Snippets with this:

function tu_estimated_reading_timeb() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 250; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = str_word_count( $clean_content );
    $time = ceil( $word_count / $wpm );

    return $time . ' min read';
}

add_filter( 'render_block', function( $block_content, $block ) {
    if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-estimated-time' ) !== false ) {
		$myreplace1 = 'Placeholder';
		$myinsert1 = tu_estimated_reading_timeb();
        $block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
    }

    return $block_content;
}, 10, 2 );

That should make them identical.

This archived topic is closed to new replies.