You could add a custom template when certain URL patterns are matched and then print on it whatever you want using your own functions to return your database query results dynamically.
The following sample adds a new custom template suggestion. So that always when the URL contains foobar
as first segment and a non-empty second segment the foobar.php
template from your current theme will be rendered.
add_action('template_include', 'template_suggestions');
function template_suggestions($template) {
$segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$foobar_template = locate_template(['foobar.php']);
if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar' && $foobar_template !== '') {
status_header(200);
return $foobar_template;
}
return $template;
}
Now use any other custom function to return your database query results.
function foobar_results() {
$myrows = [];
$segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (isset($segments[1]) && isset($segments[2]) && $segements[1] === 'foobar') {
global $wpdb;
$myrows = $wpdb->get_results( "SELECT * FROM mytable WHERE id = {$segments[2]}" );
}
return $myrows;
}
And then in foobar.php
simply loop through the results.
<?php foreach(foobar_results() as $row): ?>
<?php print $row->id; ?>
<?php endforeach; ?>