Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 966
scott Avatar asked Apr 18 '13 09:04

scott


People also ask

How do I add a post title in WordPress?

This block is primarily nested inside a query loop block and helps to customize the appearance of the query loop. To add a Post Title block, click the Block Inserter icon when editing the page template. Search for the Post Title block. Click on it to add the block to your page template.

How do I show page titles in WordPress?

Click the title bar in the upper left corner to expose the Tools menu, then choose Global Settings, or just use the keyboard shortcut ⌘+U (Mac) or Ctrl+U (Windows). On the General tab, navigate to the Default Page Heading section. To display the WordPress page title, set Show to Yes.


1 Answers

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.

like image 93
Sepster Avatar answered Sep 28 '22 07:09

Sepster