📄 functions.php
字号:
" GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] );
update_post_caches($posts);
return $posts;
}
function &query_posts($query) {
global $wp_query;
return $wp_query->query($query);
}
function update_post_cache(&$posts) {
global $post_cache;
if ( !$posts )
return;
for ($i = 0; $i < count($posts); $i++) {
$post_cache[$posts[$i]->ID] = &$posts[$i];
}
}
function clean_post_cache($id) {
global $post_cache;
if ( isset( $post_cache[$id] ) )
unset( $post_cache[$id] );
}
function update_page_cache(&$pages) {
global $page_cache;
if ( !$pages )
return;
for ($i = 0; $i < count($pages); $i++) {
$page_cache[$pages[$i]->ID] = &$pages[$i];
wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
}
}
function clean_page_cache($id) {
global $page_cache;
if ( isset( $page_cache[$id] ) )
unset( $page_cache[$id] );
}
function update_post_category_cache($post_ids) {
global $wpdb, $category_cache;
if ( empty($post_ids) )
return;
if ( is_array($post_ids) )
$post_ids = implode(',', $post_ids);
$dogs = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat WHERE post_id IN ($post_ids)");
if ( empty($dogs) )
return;
foreach ($dogs as $catt)
$category_cache[$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
}
function update_post_caches(&$posts) {
global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache;
global $wpdb;
// No point in doing all this work if we didn't match any posts.
if ( !$posts )
return;
// Get the categories for all the posts
for ($i = 0; $i < count($posts); $i++) {
$post_id_array[] = $posts[$i]->ID;
$post_cache[$posts[$i]->ID] = &$posts[$i];
$comment_count_cache[$posts[$i]->ID] = $posts[$i]->comment_count;
}
$post_id_list = implode(',', $post_id_array);
update_post_category_cache($post_id_list);
// Get post-meta info
if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
// Change from flat structure to hierarchical:
$post_meta_cache = array();
foreach ($meta_list as $metarow) {
$mpid = $metarow['post_id'];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];
// Force subkeys to be array type:
if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
$post_meta_cache[$mpid] = array();
if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
$post_meta_cache[$mpid]["$mkey"] = array();
// Add a value to the current pid/key:
$post_meta_cache[$mpid][$mkey][] = $mval;
}
}
}
function update_category_cache() {
return true;
}
function wp_head() {
do_action('wp_head');
}
function wp_footer() {
do_action('wp_footer');
}
function is_single ($post = '') {
global $wp_query;
if ( !$wp_query->is_single )
return false;
if ( empty( $post) )
return true;
$post_obj = $wp_query->get_queried_object();
if ( $post == $post_obj->ID )
return true;
elseif ( $post == $post_obj->post_title )
return true;
elseif ( $post == $post_obj->post_name )
return true;
return false;
}
function is_page ($page = '') {
global $wp_query;
if ( !$wp_query->is_page )
return false;
if ( empty($page) )
return true;
$page_obj = $wp_query->get_queried_object();
if ( $page == $page_obj->ID )
return true;
elseif ( $page == $page_obj->post_title )
return true;
else if ( $page == $page_obj->post_name )
return true;
return false;
}
function is_attachment () {
global $wp_query;
return $wp_query->is_attachment;
}
function is_preview() {
global $wp_query;
return $wp_query->is_preview;
}
function is_archive () {
global $wp_query;
return $wp_query->is_archive;
}
function is_date () {
global $wp_query;
return $wp_query->is_date;
}
function is_year () {
global $wp_query;
return $wp_query->is_year;
}
function is_month () {
global $wp_query;
return $wp_query->is_month;
}
function is_day () {
global $wp_query;
return $wp_query->is_day;
}
function is_time () {
global $wp_query;
return $wp_query->is_time;
}
function is_author ($author = '') {
global $wp_query;
if ( !$wp_query->is_author )
return false;
if ( empty($author) )
return true;
$author_obj = $wp_query->get_queried_object();
if ( $author == $author_obj->ID )
return true;
elseif ( $author == $author_obj->nickname )
return true;
elseif ( $author == $author_obj->user_nicename )
return true;
return false;
}
function is_category ($category = '') {
global $wp_query;
if ( !$wp_query->is_category )
return false;
if ( empty($category) )
return true;
$cat_obj = $wp_query->get_queried_object();
if ( $category == $cat_obj->cat_ID )
return true;
else if ( $category == $cat_obj->cat_name )
return true;
elseif ( $category == $cat_obj->category_nicename )
return true;
return false;
}
function is_search () {
global $wp_query;
return $wp_query->is_search;
}
function is_feed () {
global $wp_query;
return $wp_query->is_feed;
}
function is_trackback () {
global $wp_query;
return $wp_query->is_trackback;
}
function is_admin () {
global $wp_query;
return ( $wp_query->is_admin || strstr($_SERVER['REQUEST_URI'], 'wp-admin/') );
}
function is_home () {
global $wp_query;
return $wp_query->is_home;
}
function is_404 () {
global $wp_query;
return $wp_query->is_404;
}
function is_comments_popup () {
global $wp_query;
return $wp_query->is_comments_popup;
}
function is_paged () {
global $wp_query;
return $wp_query->is_paged;
}
function in_the_loop() {
global $wp_query;
return $wp_query->in_the_loop;
}
function get_query_var($var) {
global $wp_query;
return $wp_query->get($var);
}
function have_posts() {
global $wp_query;
return $wp_query->have_posts();
}
function rewind_posts() {
global $wp_query;
return $wp_query->rewind_posts();
}
function the_post() {
global $wp_query;
$wp_query->the_post();
}
function get_theme_root() {
return apply_filters('theme_root', ABSPATH . "wp-content/themes");
}
function get_theme_root_uri() {
return apply_filters('theme_root_uri', get_settings('siteurl') . "/wp-content/themes", get_settings('siteurl'));
}
function get_stylesheet() {
return apply_filters('stylesheet', get_settings('stylesheet'));
}
function get_stylesheet_directory() {
$stylesheet = get_stylesheet();
$stylesheet_dir = get_theme_root() . "/$stylesheet";
return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
}
function get_stylesheet_directory_uri() {
$stylesheet = rawurlencode( get_stylesheet() );
$stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
}
function get_stylesheet_uri() {
$stylesheet_dir_uri = get_stylesheet_directory_uri();
$stylesheet_uri = $stylesheet_dir_uri . "/style.css";
return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
}
function get_template() {
$template = get_settings('template');
if (!file_exists(get_theme_root() . "/$template")) { //works for dirs too
update_option('template', 'default');
update_option('stylesheet', 'default');
}
return apply_filters('template', get_settings('template'));
}
function get_template_directory() {
$template = get_template();
$template_dir = get_theme_root() . "/$template";
return apply_filters('template_directory', $template_dir, $template);
}
function get_template_directory_uri() {
$template = get_template();
$template_dir_uri = get_theme_root_uri() . "/$template";
return apply_filters('template_directory_uri', $template_dir_uri, $template);
}
function get_theme_data($theme_file) {
$theme_data = implode('', file($theme_file));
preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
preg_match("|Description:(.*)|i", $theme_data, $description);
preg_match("|Author:(.*)|i", $theme_data, $author_name);
preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
preg_match("|Template:(.*)|i", $theme_data, $template);
if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
$version = trim($version[1]);
else
$version ='';
if ( preg_match("|Status:(.*)|i", $theme_data, $status) )
$status = trim($status[1]);
else
$status = 'publish';
$description = wptexturize(trim($description[1]));
$name = $theme_name[1];
$name = trim($name);
$theme = $name;
if ( '' == $author_uri[1] ) {
$author = trim($author_name[1]);
} else {
$author = '<a href="' . trim($author_uri[1]) . '" title="' . __('Visit author homepage') . '">' . trim($author_name[1]) . '</a>';
}
return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1], 'Status' => $status);
}
function get_themes() {
global $wp_themes;
global $wp_broken_themes;
if ( isset($wp_themes) )
return $wp_themes;
$themes = array();
$wp_broken_themes = array();
$theme_root = get_theme_root();
$theme_loc = str_replace(ABSPATH, '', $theme_root);
// Files in wp-content/themes directory
$themes_dir = @ dir($theme_root);
if ( $themes_dir ) {
while(($theme_dir = $themes_dir->read()) !== false) {
if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' ) {
continue;
}
$stylish_dir = @ dir($theme_root . '/' . $theme_dir);
$found_stylesheet = false;
while (($theme_file = $stylish_dir->read()) !== false) {
if ( $theme_file == 'style.css' ) {
$theme_files[] = $theme_dir . '/' . $theme_file;
$found_stylesheet = true;
break;
}
}
if ( !$found_stylesheet ) {
$wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
}
}
}
}
if ( !$themes_dir || !$theme_files ) {
return $themes;
}
sort($theme_files);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -