2

I'm making a wp site as a new frontend to an in house system that I designed a while back.

I want to be able to run custom php for grabbing different bits and peices of stuff from my database (mysql) and displaying dynamic data etc , but can't seem to do this.

I thought the code snippets plugin may have helped, but whilst that does run custom PHP, its not call-able on a per page basis, but rather runs no matter where I am, which is not suitable.

Any help appriciated.

  • Are you using a custom or child theme? – socki03 Oct 24 '17 at 15:32
  • I'm using a custom theme called 'sydney'... well I say custom... I got it from the theme library :) – John Oct 24 '17 at 15:33
  • I'd recommend creating a child theme on top of Sydney as to not mess with the base code, unless you are never going to update the theme. – socki03 Oct 24 '17 at 15:38
  • Shortcodes and page templates! – Tom J Nowell Oct 24 '17 at 15:46
2

If you are using a child or custom theme, I encourage you to look at the Codex's Template Hierarchy.

The short answer to your question is yes, you can run custom code on any page you'd like.

The longer answer is still yes, but it's a bit more involved in what your end product is going to look like.

I'll paste in the part about pages from the aforementioned page:

The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following path:

  • custom template file – The page template assigned to the page. See get_page_templates().
  • page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php.
  • page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php.
  • page.php
  • singular.php
  • index.php

So, you have the ability to create any one of these custom templates, and apply them to your page, running any code you need.

  • so say for example I run a bit of code and go fetch a load of stuff from my db and assign it to variables in php, how do i then show that on page? – John Oct 24 '17 at 15:51
  • Depends on how you want to lay it out, but usually using a while or foreach loop, with echo or print – socki03 Oct 24 '17 at 15:54

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.