Site logo

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

Home Forums Support [Resolved] Can I bring custom dynamic content from posts into a query loop?

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

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #2458343
    Michelle

    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?

    #2458360
    Michelle

    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.

    #2458378
    Fernando
    Customer Support

    Hi Michelle,

    Can you provide the link to your Blog page? I’ll check how it’s currently set up.

    You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2458447
    Michelle

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

    #2458462
    Fernando
    Customer Support

    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.

    #2459611
    Michelle

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

    #2459789
    Fernando
    Customer Support

    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.

    #2460887
    Michelle

    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)

    #2461058
    Fernando
    Customer Support

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

    #2461091
    Michelle

    Yes, go ahead.

    #2461103
    Fernando
    Customer Support

    I was able to add it in Code Snippets.

    Now, just add a GB Headline Block with text Placeholder and a class cu-estimated-time. Example: https://share.getcloudapp.com/WnuZJZkX

    #2461112
    Michelle

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

    #2461117
    Fernando
    Customer Support

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

    #2461123
    Michelle

    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.

    #2461131
    Fernando
    Customer Support

    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.

Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.