Collections Template

– Store PHP code in a dedicated page template file.

Perform project setup first, step 2. Setup.

This technique shows how to put PHP code in pages, not posts. All pages use a template file as its base. Template files live in the theme root directory. They can be shared by several pages, or as in this case, dedicated to one page.

I think using template files is the superior method of programming a page, because you can use a code editor, it is the most flexible, and can be the basis of a full web application inside of WordPress. Additionally, the PHP code runs directly, it doesn’t need the Insert PHP plugin. There is a performance drawback using the Insert PHP plugin: the code runs in an 'eval()'' operation; some people avoid eval for security reasons, eval’d code runs more slowly, the code doesn’t integrate so well with other PHP code, etc.

I think is it acceptable for small pieces of code like the previous examples to be inserted into Page text boxes like I have shown, but for larger integration programs, use a page template.

As far as the WordPress Page, this time there is no text stored there, it is just a placeholder. It is linked to the new template file to display everything.

This example has the same query as Collections and Collections Include , but I tweaked the colors a little.

Page will display:

Color Field
Minimalism
Sculpture
Symbolism and Symbology
Transcontinental Legacy

There are three steps to build this dedicated file method:

  • Clone the template page.php file.
  • Insert the PHP code in the new file.
  • Create a placeholder Page in WordPress.


1. Clone the page.php file. This is the default template file in WordPress themes. We copy it to a new name, let’s use collections_page_template.php, in your theme or your child theme directory.

cp page.php collections_page_template.php

Code

2. Use a file editor to edit collections_page_template.php. In the top comment area, add a new comment to identify the template name. The name can be anything, we will use it in step 3.

Template Name: Collections Page Template

Next, in the loop find the line:

1
<?php while ( have_posts() ) : the_post(); ?>

Just before that line, copy and paste this code:

<?php
$query = new AirpressQuery();
$query->setConfig("Artists_DB");
$query->sort("Name", "asc");
$query->table("Collections");
$data = new AirpressCollection($query);
if (count($data)){
  echo '<table style="table-layout:fixed; width:50%;">';
  foreach($data as $row){
    echo '<tr><td style="border-color:#4d8fb4; 
          background-color:#eeeeff">'.
          $row["Name"].'</td></tr>';
  }
  echo '</table>';
}
?>

3. In the WordPress Admin, Pages, create a new page. You can name it Collections Page. In the Page Attributes section, choose from the Template selector your new 'Collection Page Template'. And that is all, you don’t need anything in the edit box. Update and view this page to see the Airtable data.