function cfct_choose_content_template_role($type = 'content', $files = null) { $files = cfct_role_templates($type, $files); if (count($files)) { $user = new WP_User(get_the_author_ID()); if (count($user->roles)) { foreach ($files as $file) { foreach ($user->roles as $role) { if (cfct_role_filename_to_name($file) == $role) { $keys = array($role); return cfct_filename($type, 'role', $keys); } } } } } return false; }
/** * Chooses which template to display for the single context based on user role * * @param string $dir Directory to use for selecting the template file * @param array $files A list of files to search through to find the correct template * @param string $filter Used in filtering the filename * @return mixed Path to the file, false if no file exists * **/ function cfct_choose_single_template_role($dir, $files, $filter) { $role_files = cfct_role_templates($dir, $files, $filter); if (count($role_files)) { $user = new WP_User(get_the_author_meta('ID')); if (count($user->roles)) { foreach ($role_files as $file) { $file = cfct_filename_filter($file, $filter); foreach ($user->roles as $role) { if (cfct_role_filename_to_name($file) == $role) { return $file; } } } } } return false; }
function cfct_choose_single_template($files = array(), $filter = '*') { // must be called within the_loop - cfct_choose_general_template_single() approximates a loop for this reason. $exec_order = array('author', 'meta', 'category', 'role', 'tag', 'parent', 'default'); $exec_order = apply_filters('cfct_single_match_order', $exec_order); $filename = false; global $post; foreach ($exec_order as $type) { switch ($type) { case 'author': $author_files = cfct_author_templates('', $files); if (count($author_files)) { $author = get_the_author_login(); $file = cfct_filename_filter('author-' . $author . '.php', $filter); if (in_array($file, $author_files)) { $filename = $file; } } break; case 'meta': $meta_files = cfct_meta_templates('', $files); if (count($meta_files)) { $meta = get_post_custom($post->ID); if (count($meta)) { // check key, value matches first foreach ($meta as $k => $v) { $val = $v[0]; $file = cfct_filename_filter('meta-' . $k . '-' . $val . '.php', $filter); if (in_array($file, $meta_files)) { $filename = $file; break; } } // check key matches only if (!$filename) { foreach ($meta as $k => $v) { $file = cfct_filename_filter('meta-' . $k . '.php', $filter); if (in_array($file, $meta_files)) { $filename = $file; break; } } } } } break; case 'category': $cat_files = cfct_cat_templates($type, $files); if (count($cat_files)) { foreach ($cat_files as $file) { $cat_id = cfct_cat_filename_to_id($file); if (in_category($cat_id)) { $filename = $file; break; } } } break; case 'role': $role_files = cfct_role_templates($type, $files); if (count($role_files)) { $user = new WP_User(get_the_author_ID()); if (count($user->roles)) { foreach ($role_files as $file) { foreach ($user->roles as $role) { if (cfct_role_filename_to_name($file) == $role) { $filename = $file; break; } } } } } break; case 'tag': $tag_files = cfct_tag_templates($type, $files); if (count($tag_files)) { $tags = get_the_tags($post->ID); if (is_array($tags) && count($tags)) { foreach ($tag_files as $file) { foreach ($tags as $tag) { if ($tag->slug == cfct_tag_filename_to_name($file)) { $filename = $file; break; } } } } } break; case 'parent': $parent_files = cfct_parent_templates($type, $files); if (count($parent_files) && $post->post_parent > 0) { $parent = cfct_post_id_to_slug($post->post_parent); $file = cfct_filename_filter('parent-' . $parent . '.php', $filter); if (in_array($file, $parent_files)) { $filename = $file; } } break; case 'default': break; } if ($filename) { break; } } return apply_filters('cfct_choose_single_template', $filename); }