Pretty much all of the content on the site is going to be dynamically generated (php + db backend)

What is the preferred "wordpress way" of passing data to a php template (set up as a template in a wordpress theme)

So the template would be something like




and I'd need to be able to pass it page for each different page

Edit 1

So I have a page that, for simplicity, looks something like this

Hi everyone, this is page X The content here is generated from a db call:


[Spit out db resultset here]

and the template would be something like



$query='SELECT * FROM db WHERE page=X'
foreach($result as $row){
echo $row

  • 1. What is the 'template'? Is it a html skeleton that is later filled with content generated by php? – KalenGi Jul 23 '12 at 4:38
  • 2. What sort of content are you displaying on your pages? Is it articles (posts) or some other type of content that cannot be produced by the regular WordPress posts mechanism? – KalenGi Jul 23 '12 at 4:41
  • Its basically a dump of a db table (for now) The template is actually a custom template -- I copied the source from template:squirrel/page.php and added in some db statements after the the_content() call – arcyqwerty Jul 23 '12 at 4:50
  • Basically a lot of the (nonstatic) content has to be generated in php. Ideally, I'd just want a wordpress shell (to manage users/styles/comments and such) with PHP content in place of all the static 'posts' – arcyqwerty Jul 23 '12 at 4:51

If you want to produce a page in WordPress whose structure and content are dynamically generated independent of the regular posts handling mechanism then:

  1. Create a new page with title "My Custom Page". This generates the page slug 'my-custom-page' by default.

  2. In the root of your active theme folder eg. \wp-content\themes\twentyeleven\ create a page template for this new page named page-my-custom-page.php.

  3. Put your custom php code inside the page template.

  4. Navigate to your-site.com/my-custom-page/ to see the results

|improve this answer|||||
  • So this is similar to what I have already done (create a page based on a custom PHP template). My question is, can I create several more pages based on the same template such that each page pulls different info from the database – arcyqwerty Jul 23 '12 at 13:42
  • 1
    Yes. You simply pass a query argument to the page (eg. your-site.com/my-custom-page/?thepage=2) and then it picks data based on the value of the query argument. – KalenGi Jul 23 '12 at 19:49
  • I want it to be more like mysite.com/pageX and mysite.com/pageY where the variable would be hardcoded in on the backend. PageX calls loads the template with argument 'X' whereas PageY loads it with argument 'Y' – arcyqwerty Jul 23 '12 at 19:58
  • In that case you might want to look into wp_rewrite. It does a good job of handling custom urls – KalenGi Jul 23 '12 at 21:54
  • Is there no way in WP just to have it do something like URL routing (/page/:id style) or use a render template or partial views or anything? – arcyqwerty Jul 24 '12 at 1:31

You're asking a pretty vague question, but I think you should check out the WordPress Template Hierarchy, it might help you get a better understanding of how WordPress behaves.

You can use page-$slug.php or page-$id.php to automatically include a page-specific template.

|improve this answer|||||
  • Yeah, I already found that through google. – arcyqwerty Jul 23 '12 at 3:37
  • I mean, I have a page that's based off of a template. But that template needs to change its db query based on which page is being loaded – arcyqwerty Jul 23 '12 at 3:37
  • @arcyqwerty Huh? So confused. – Brian Fegter Jul 23 '12 at 3:39
  • So I have created a page and a template. The page uses this template but needs to tell the template code what to query from the database – arcyqwerty Jul 23 '12 at 3:40
  • I'll edit question – arcyqwerty Jul 23 '12 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.