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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With