How to include page title of wordpress post in the content possibly with a shortcode

scott picture scott · Apr 18, 2013 · Viewed 38.6k times · Source

in the wordpress admin, i would like to do the following when creating a page:

Page Title: Test

Page content:

Lorem ipsum dolor [page_title] sit amet, consectetur adipiscing elit. Nunc et lectus sit amet ante vulputate ultrices at sit amet [page_title] tortor. Nam mattis commodo mi in semper. Suspendisse ut eros dolor. Morbi at odio feugiat [page_title] nunc vestibulum venenatis sit amet vitae neque. Nam ullamcorper ante ac risus malesuada id iaculis nibh ultrices.


Where it says [page_title] I would like it to print the page title (Test)

This needs to be achieved through the admin system, not hard-coded in the template.

Answer

Sepster picture Sepster · Apr 18, 2013

Refer to the codex: Shortcode API

function myshortcode_title( ){
   return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );

Add this to your theme's functions.php file.

Note that per the comments exchange between S.Visser and I in his answer - this solution will only work inside The Loop, while his will also work outside The Loop and so his is the more complete answer.