Created
January 25, 2012 14:42
-
-
Save kozo002/1676574 to your computer and use it in GitHub Desktop.
WordPress Pluginでテンプレートを操作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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