1

I have a custom template set on all my Pages, it loads the template fine, now I need to switch into fetching data from the database based on the post name.

mysite.com/cars -> page name [cars]

Page [cars] loads the home.php template:

home.php

<?php 

get_header();

//get name of page [cars] use it to retrieve from a custom table 
//with the primary key [cars]

get_footer();
?>

do I have to parse the URL to extract the name or is there someway I can provide wordpress the name. I dont see an option to set it on the posts page.

Undermine2k
  • 1,481
  • 4
  • 28
  • 52

1 Answers1

1

You're going to want

$pagename

The WP global variable $pagename should be available for you.

$pagename is defined in the file wp-includes/theme.php, inside the function get_page_template(), which is of course called before your page theme files are parsed, so it is available at any point inside your templates for pages.

  • Although it doesn't appear to be documented, the $pagename var is only set if you use permalinks. I guess this is because if you don't use them, WP doesn't need the page slug, so it doesn't set it up.

  • $pagename is not set if you use the page as a static front page.

see How to get the current page name in WordPress?

Community
  • 1
  • 1
chrismillah
  • 3,704
  • 2
  • 14
  • 20