HelmBurger
HelmBurger

Reputation: 1298

How to get the title of wordpress post

I know there is a question with similar title but my question is different. How can I get the title of wordpress post. I know the_title() returns AND prints the title of the post. But I don't want it to print. I just want to take it in a PHP variable that I can use later on. Anybody knows?

Upvotes: 0

Views: 50

Answers (2)

Script47
Script47

Reputation: 14530

Look in to the link below which is available in the wordpress docs.

This function will return the title of a post for a given post ID. If the post is protected or private, the word "Protected: " or "Private: " will be prepended to the title. It can be used inside or outside of The Loop. If used outside the loop an ID must be specified.

Quoted from the link below.

https://codex.wordpress.org/Function_Reference/get_the_title

Upvotes: 1

developerwjk
developerwjk

Reputation: 8659

Capture it with the ob_ functions:

ob_start();
the_title()
$title = ob_get_clean();

What this does, is you print it, but only after starting an output buffer, so you print it to that buffer, then get it from the buffer and close the buffer.

Upvotes: 0

Related Questions