get_option()获取wp_option表中的选项/值对

目录

描述

译文

这是一种从选项数据库表中获取有名称的选项的值的安全方法。如果所需选项不存在,或选项无相关值,函数返回FALSE。

原文

A safe way of getting values for a named option from the options database table. If the desired option does not exist, or no value is associated with it, FALSE will be returned.

get_option() 描述

用法

 <?php echo get_option$option$default ); ?> 

get_option() 用法

参数

$option

(string) (必填) Name of the option to retrieve. Underscores separate words, lowercase only.

默认值: None

A concise list of valid options is below, but a more complete one can be found at the Option Reference.

  • 'admin_email' - E-mail address of blog administrator.
  • 'blogname' - Weblog title; set in General Options.
  • 'blogdescription' - Tagline for your blog; set in General Options.
  • 'blog_charset' - Character encoding for your blog; set in Reading Options.
  • 'date_format' - Default date format; set in General Options.
  • 'default_category' - Default post category; set in Writing Options.
  • 'home' - The blog's home web address; set in General Options.
  • 'siteurl' - WordPress web address; set in General Options.
    Warning: This is not the same as get_bloginfo('siteurl') (which will return the homepage url), but as get_bloginfo('wpurl').
  • 'template' - The current theme's name; set in Presentation.
  • 'start_of_week' - Day of week calendar should start on; set in General Options.
  • 'upload_path' - Default upload location; set in Miscellaneous Options.
  • 'users_can_register' - Whether users can register; set in General Options.
  • 'posts_per_page' - Maximum number of posts to show on a page; set in Reading Options.
  • 'posts_per_rss' - Maximum number of most recent posts to show in the syndication feed; set in Reading Options.

There are many more options available, a lot of which depend on what plugins you have installed.

$default

(mixed) (可选) The default value to return if no value is returned (ie. the option is not in the database).

默认值: false

get_option() 参数

返回值

(mixed) 

Current value for the specified option. If the option does not exist, returns parameter $default if specified or boolean FALSE by default.

get_option() 返回值

示例


<?php

$no_exists_value get_option'no_exists_value' );
var_dump$no_exists_value ); /* outputs false */

$no_exists_value get_option'no_exists_value''default_value' );
var_dump$no_exists_value ); /* outputs 'default_value' */

?>

Show Blog Title

Displays your blog's title in a <h1> tag.

 <h1><?php echo get_option'blogname' ); ?></h1> 

Show Character Set

Displays the character set your blog is using (ex: UTF-8)

 <p>Character set: <?php echo get_option'blog_charset' ); ?> </p> 

Retrieve Administrator E-mail

Retrieve the e-mail of the blog administrator, storing it in a variable.

 <?php $admin_email get_option'admin_email' ); ?> 

get_option() 示例

注意

  • 使用到: apply_filters()
    • 调用 'pre_option_$option' before checking the option. Any value other than false will "short-circuit" the retrieval of the option and return the returned value. You should not try to override special options, but you will not be prevented from doing so.
    • 调用 'option_$option', after checking the option, with the option value.
    • 调用 'default_option_$option' to 过滤器 $default before returning it if it is set and the value doesn't exist.
  • 使用到: maybe_unserialize() to unserialize the value before returning it.

get_option() 注意

源文件

get_option() 函数的代码位于 wp-includes/option.php.

get_option() 源文件

相关