I’m pretty sure that it’s not possible but can I get the result of wp_title()
into a variable if I know post’s ID?
So, for instance I’m on a “blog” page and I want to have the title of “about” page in a variable (not in the <title>
tag like in this questions: Setting title using wp_title filter).
As you said,
wp_title
works only for current post, so can be a little tricky save it in a variable for a post that is not the current.However,
wp_title
works not only for singular post / page / cpt but also for every type of archive. So it’s easy create a custom function that copy the part of the core function that regard the single post / page.Code is in large part copied fron the core
wp_title
function.Note that all filters defined for
wp_title
also works for this function.wp_title() function has a second parameter ‘display’ . Set it to false to get the title in a variable :
If you want to get the title by page ID :