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]:
-
2Please elaborate, what is the problem?– JohanssonCommented Aug 16, 2018 at 6:49
-
wall of code is not an actual question– Mark KaplunCommented Aug 18, 2018 at 14:22
1 Answer
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.
-
Please replace [30 Second Read] get_post_meta($post->ID, 'm_meta_description', true)– Wakil DJCommented Aug 16, 2018 at 15:06
-
1while 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. Commented Aug 18, 2018 at 14:20
-
1Point 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.– TedinozCommented Aug 19, 2018 at 1:43