wordpress get_option()函数

Wanda ·
更新时间:2024-09-20
· 648 次阅读

【函数介绍】

get_option()用于获取通过option表单设置值得方法,如果数据库中不存在该选项,或者该选项的值为空,则返回FALSE.

【函数使用】 【参数说明】

$option
(string) (必须) 查询选项的名称. 数据库中已存在的选项名称如下(这些都在后台的“菜单”=》“常规”):

'admin_email' – 管理员的E-mail地址. 'blogname' – 网站title标题. 'blogdescription' – 网站描述. 'blog_charset' – 网站编码一般UTF-8. 'date_format' – 日期格式. 'default_category' – 文章默认分类. 'home' – 网站地址. 'siteurl' – wordpress的web地址.
主题: 此选项与函数 get_bloginfo('siteurl')不同 get_bloginfo('siteurl')返回当前站地址, 但是这个选项等同于get_bloginfo('wpurl');. 'template' -当前主题名称 'start_of_week' -一星期开始设置 'upload_path' – 上传默认目录. 'posts_per_page' – 文章分页每页显示的数量. 'posts_per_rss' – RSS聚合显示的最新文章数量

默认: None

$default
(mixed) (可选) 当数据库中不存在该选项默认的返回值.
Default: false

【返回值】

指定选项名称的值,如果没有返回数组则返回FALSE.更多信息请见http://codex.wordpress.org/Option_Reference

【函数实例】

显示博客标题:

显示博客站点字符集:

Character set:

获取管理员email地址:

【源代码】

get_option() 位于 wp-includes/option.php,函数代码如下:

function get_option( $option, $default = false ) { global $wpdb; $option = trim( $option ); if ( empty( $option ) ) return false; // Allow plugins to short-circuit options. $pre = apply_filters( 'pre_option_' . $option, false ); if ( false !== $pre ) return $pre; if ( defined( 'WP_SETUP_CONFIG' ) ) return false; if ( ! defined( 'WP_INSTALLING' ) ) { // prevent non-existent options from triggering multiple queries $notoptions = wp_cache_get( 'notoptions', 'options' ); if ( isset( $notoptions[$option] ) ) return apply_filters( 'default_option_' . $option, $default ); $alloptions = wp_load_alloptions(); if ( isset( $alloptions[$option] ) ) { $value = $alloptions[$option]; } else { $value = wp_cache_get( $option, 'options' ); if ( false === $value ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); // Has to be get_row instead of get_var because of funkiness with 0, false, null values if ( is_object( $row ) ) { $value = $row->option_value; wp_cache_add( $option, $value, 'options' ); } else { // option does not exist, so we must cache its non-existence $notoptions[$option] = true; wp_cache_set( 'notoptions', $notoptions, 'options' ); return apply_filters( 'default_option_' . $option, $default ); } } } } else { $suppress = $wpdb->suppress_errors(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); $wpdb->suppress_errors( $suppress ); if ( is_object( $row ) ) $value = $row->option_value; else return apply_filters( 'default_option_' . $option, $default ); } // If home is not set use siteurl. if ( 'home' == $option && '' == $value ) return get_option( 'siteurl' ); if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) ) $value = untrailingslashit( $value ); return apply_filters( 'option_' . $option, maybe_unserialize( $value ) ); }



get 函数 WordPress option

需要 登录 后方可回复, 如果你还没有账号请 注册新账号