-2
function wpd_title_prefix_filter( $title, $post_id ) {
    if( $checked = get_post_meta( $post_id, '_wpd_title_prefix', true ) ){
        $title = '[30 Second Read]: ' . $title;
    }
    return $title;
}
add_filter( 'the_title', 'wpd_title_prefix_filter', 10, 2 );

$title = ' ' . $title; [30 Second Read]:
  • 2
    Please elaborate, what is the problem? – Jack Johansson Aug 16 '18 at 6:49
  • wall of code is not an actual question – Mark Kaplun Aug 18 '18 at 14:22
0

You face two challenges in order to achieve your goal.

The first is very simple. If one can be sure that the reading time for every post is 30 seconds, then your function needs an "else" statement to define the reading time as a suffix. The following would work.

function wpd_title_prefix_filter( $title, $post_id ) {
    if( $checked = get_post_meta( $post_id, 'm_meta_description', true ) ){
        $title = '[30 Second Read]: ' . $title;
    }
        else{
         $title = $title.' [30 Second Read]';
        }
    return $title;
}

Your second challenge is more complicated - what if you don't know the reading time. This is well covered in a post titled "How to Display Estimated Post Reading Time in Your WordPress Posts" on wp.beginner.com. It requires a plugin ("Reading Time WP"); the article has full step-by-step details.

Updated at request of OP to change the name of the postmeta key.
I left $post_id as-is because this is a function parameter.

| improve this answer | |
  • Please replace [30 Second Read] get_post_meta($post->ID, 'm_meta_description', true) – Wakil DJ Aug 16 '18 at 15:06
  • 1
    while there is nothing wrong with changing your code by request, in principal the aim of the site is to educate, not to deliver ready to run code. – Mark Kaplun Aug 18 '18 at 14:20
  • 1
    Point taken. With the benefit of hindsight, I think your and Jack Johansson's comments on the original question were 100% correct. Is this a problem with PHP or WordPress; had the OP done any productive research already? The question should have been put on hold (at least). I did wonder myself, but my profile did not extend to making comments on questions and I was too keen to be helpful, and didn't draw the line. Hopefully one (and I mean me, in this case) learns from one's mistakes. – Tedinoz Aug 19 '18 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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