Skip to content

Instantly share code, notes, and snippets.

@kozo002
Created January 25, 2012 14:42
Show Gist options
  • Select an option

  • Save kozo002/1676574 to your computer and use it in GitHub Desktop.

Select an option

Save kozo002/1676574 to your computer and use it in GitHub Desktop.
WordPress Pluginでテンプレートを操作
<?php
add_action('template_redirect', 'sample_action');
function sample_action() {
global $wp;
echo '<pre>';
var_dump($wp->query_vars);
echo '</pre>';
$template = _parse_request_uri($wp->query_vars);
if ($template && file_exists($template)) {
include $template;
exit;
}
}
function _parse_request_uri($qv) {
$result = false;
if (count($qv) <= 0)
return false;
if ($qv['category_name'] === 'sample') {
if (count($qv) === 1) {
$result = 'index';
}
elseif ($qv['name'] === 'search' && isset($_GET['q']) && mb_strlen($_GET['q']) > 0) {
// serching function
$result = 'search';
}
elseif (preg_match("/^[0-9]+$/", $qv['name'])) {
// find function
$result = 'single';
}
else {
return false;
}
} else {
return false;
}
if ($result) {
return TEMPLATEPATH . '/sample/' . $result . '.php';
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment