0

I'd like to transform the result from %title in functions like next_post_link().

More specificaly, I created a function called short_title($title, $after, $length). The idea is to return the title with an $after when the title exceeds $length words. The function works like it's supposed to do.

However, I have a hard time invoking that function.

I'm trying

<?php previous_post_link( '%link', '<span>< ' . short_title( '%title', '...', 2) . '</span>' ); ?>

but that doesn't work and doesn't either when not using the ticks around %title.

Can you please explain me how to correctly invoke my function?

Thanks

2

Unfortunately, your logic doesn't quite work out. The reason is because the order of operations, just like math, works inside to outside.

So, your short_title() function is running with just passing the string %title, which then returns, I assume, the same string because that string meets your requirements. Then it passes %title onto previous_post_link and returns the full title as it did before.

As for a solution, outside of writing your own custom function, there is a filter previous_post_link that you could try to use, but if you're only worried about the display side, you may be able to fix it with CSS using text-overflow.

  • Thanks socki. That's exactly what I did. Created a child function, created a new function in functions.php and then added to previous_post_link. Thanks – Charles Dec 4 '16 at 21:32

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.