• Resolved joshyhud

    (@joshyhud)


    Hi

    I have a page that has a for each loop for post links, but when using the <?php echo do_shortcode(‘[rt_reading_time]’); ?> the returned value is the same for all posts shown.

    however if i use an if while loop it works correctly, is there an issue with using this plugin shortcode in foreach loops or something i have missed?

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

    (@yingling017)

    If you’re using a for each loop the global $post is going to be the wrapper page you’re on. So a function like get_the_ID() would return the pages ID rather than a specific post.

    You’ll have to pass the post id to the shortcode as a parameter for each post. [rt_reading_time post_id=“10”]

    Where 10 would need to be the id of the post you’re on within the foreach loop.

    The call would work using a standard loop if you’re doing the “the_post()” that sets the $post value.

    Thread Starter joshyhud

    (@joshyhud)

    Hi Jason

    Thanks for the quick response. IS it possible to use a variable set for the post ID number being used here? such as $the_id

    Thread Starter joshyhud

    (@joshyhud)

    using other post ID’s do not chnage the output on the page when using like this :

    <?php echo do_shortcode(‘[rt_reading_time post_id=”4061″)]’); ?>

    Plugin Author Jason Yingling

    (@yingling017)

    The code you shared above has an extra parentheses.

    Do you also have a link I could take a look at. Passing the post_id should tell it to calculate the reading time based on that post IDs content.

    You can see the code for it here https://github.com/yingles/reading-time-wp/blob/master/rt-reading-time.php#L242

    Thread Starter joshyhud

    (@joshyhud)

    sorry for the late reply

    I can send you a link via email if this is ok?

    Thread Starter joshyhud

    (@joshyhud)

    I have since created the read time function manually to apply to specific code loop.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Reading time error in foreach loop’ is closed to new replies.