当您需要以下拉列表形式显示许多选项时,可以在Titan Framework中使用select
类型选项。 今天,我们将在自定义管理面板,元框和主题定制器中创建一个选择类型选项。
Titan Framework中的选择类型选项
Titan Framework中的select
类型选项可通过下拉菜单帮助显示很多选项。 这就是它的样子。
此选项类型支持设置列表:
- name :此参数分配选项的显示名称。
- id :它定义一个唯一名称,该名称检索保存的选项值。
- desc :添加带有选项名称的单行描述。
- options :此参数占用值-标签对的关联数组,在下拉菜单中作为选项显示。 您甚至可以添加一个二维数组来显示分组的选项(稍后会对此进行更多介绍)。
- default :(可选)它定义选项的默认值。
- livepreview :(可选)当在主题定制器部分中添加
select
类型选项时,此参数显示更改的实时预览。 - css :(可选)每当您在管理页面和/或主题定制器部分中添加此选项时,此参数都会自动生成CSS 。
除了“选项”(本质上是array
外,所有参数均为string
类型。
选择类型选项的可用容器
您可以在内部添加此选项:
- 管理面板
- 管理员标签
- 元框
- 主题定制器部分
到目前为止,我确定您在添加任何选项类型时必须了解通用格式:
- 首先通过
getInstance()
函数获取一个实例。 - 然后通过
createOption()
函数添加一个选项。 - 最后,通过
getOption()
函数检索保存的值。
所有这些容器都可以使用Titan Framework轻松添加。 如果您不知道如何阅读本系列的前几篇文章。
在管理面板中创建选择类型选项
声明示例
让我们在管理面板中添加此选项。
<?php
/**
*
* Create select type option in an Admin Panel
*
*/
$aa_panel->createOption( array(
'id' => 'aa_select', // The ID which will be used to get the value of this option
'type' => 'select', // Type of option we are creating
'name' => 'My Select Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option', // Description of the option which will be displayed in the admin panel
'options' => array( //An array of value-label pairs which appears as options
'aa_theme_temp1' => 'Theme Template 1',
'aa_theme_temp2' => 'Theme Template 2',
'aa_theme_temp3' => 'Theme Template 3'
),
'default' => 'aa_theme_temp2'
) );
我在第9行中使用createOption()
函数在管理面板$aa_panel
添加select
类型选项。 此函数定义参数列表,即名称,id,类型,desc,选项和default 。 我为我的选项定义了唯一的ID,即aa_select
。
现在看一下第15行,我在其中添加了一组键-值对。 每对在下拉列表中添加一个单独的选项。 我创建了三个这样的对,即select
类型选项的三个条目。
因此,标签aa_theme_temp1
, aa_theme_temp2
和aa_theme_temp3
在aa_theme_temp3
添加了名为“ 主题模板1 ”,“ 主题模板2 ”和“ 主题模板3 ”的选项。
屏幕截图显示了名为Neat Options的管理面板中的My Select Option 。
用法示例
让我们检索保存的选项值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_select_val = $titan->getOption( 'aa_select' );
/**
*
* Print Admin Panel option values
*
*/
if ( 'aa_theme_temp1' == $aa_select_val ) {
echo "You've selected Theme Template #1.";
} elseif ( 'aa_theme_temp2' == $aa_select_val ) {
echo "You've selected Theme Template #2.";
} else {
echo "You've selected Theme Template #3.";
}
?>
在第3行中,通过getInstance()
函数获取一个实例。 它使用一个唯一的参数,最好是您的主题名称(即本例中的neat
)。 然后,在第6行中使用getOption()
函数。 它注册选项ID aa_select
并将其值保存在变量$aa_select_val
。
接下来,我将使用if-else
检查语句在前端打印选定的值,尽管您可以使用此选项执行许多其他操作。 根据这些:
- 如果变量
$aa_select_val
值等于标签aa_theme_temp1
则打印:' 您已选择主题模板#1。 ' - 如果变量
$aa_select_val
值等于标签aa_theme_temp2
则打印:' 您已选择主题模板#2。 ' - 如果标签的值不是
aa_theme_temp1
或aa_theme_temp2
则打印:' 您选择了主题模板#3。 '
在前端显示结果
我选择主题模板1作为我的演示设置。
让我们找出如何在前端打印保存的设置。 因此,这是屏幕截图。
在管理标签内创建选择类型选项
声明示例
现在,我将在“管理”选项卡中创建此选项,但是使用不同的方法。
<?php
/**
*
* Create select type option in an Admin Tab
*
*/
$aa_tab1->createOption( array(
'id' => 'aa_select_in_tab1_panel2', // The ID which will be used to get the value of this option
'type' => 'select', // Type of option we are creating
'name' => 'My Select Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option', // Description of the option which will be displayed in the admin panel
'options' => array( //An array of value-label pairs which appears as options
'aa_theme_temp1' => 'Theme Template 1',
'aa_theme_temp2' => 'Theme Template 2',
'aa_theme_temp3' => 'Theme Template 3'
),
'default' => 'aa_theme_temp2'
) );
我将此选项添加到ID $aa_tab1
的管理选项卡$aa_tab1
aa_select_in_tab1_panel2
。
您可以在管理面板“ 整洁的选项2”的 选项 卡1内找到“ 我的选择选项” 。
用法示例
现在,我将检索其值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_select_in_tab1_panel2_val = $titan->getOption( 'aa_select_in_tab1_panel2' );
/**
*
* Print Admin Tab option values
*
*/
if ( 'aa_theme_temp1' == $aa_select_in_tab1_panel2_val ) {
echo "You've selected Theme Template #1.";
} elseif ( 'aa_theme_temp2' == $aa_select_in_tab1_panel2_val ) {
echo "You've selected Theme Template #2.";
} else {
echo "You've selected Theme Template #3.";
}
我们通过以下步骤获得值:
- 在第3行中获取一个实例。
- 通过注册唯一ID在第6行中获取保存的值。
- 通过#16至#27行中的
if-else
检查语句在前端打印保存的值。
在前端显示结果
现在,我将选择“ 主题模板2 ”作为演示设置。 这是前端的屏幕截图。
在元框内创建选择类型选项
声明示例
这是代码:
<?php
/**
*
* Create select type option in a Metabox
*
*/
$aa_metbox->createOption( array(
'id' => 'aa_mb_select', // The ID which will be used to get the value of this option
'type' => 'select', // Type of option we are creating
'name' => 'My Select Option', // Name of the option which will be displayed
'desc' => 'This is our option', // Description of the option which will be displayed
'options' => array( //An array of value-label pairs which appears as options
'aa_theme_temp1' => 'Theme Template 1',
'aa_theme_temp2' => 'Theme Template 2',
'aa_theme_temp3' => 'Theme Template 3'
),
'default' => 'aa_theme_temp2'
) );
在这里,我将在ID为aa_mb_select
$aa_metbox
添加select
类型选项。 在这里,我使用的是在管理面板中定义的相同的关联数组。
在页面编辑屏幕的结尾,在metabox Metabox Options内显示了一个名为My Select Option的下拉菜单。
用法示例
让我们获取值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_mb_select_val = $titan->getOption( 'aa_mb_select', get_the_ID() );
/**
*
* Print Metabox option values
*
*/
if ( 'aa_theme_temp1' == $aa_mb_select_val ) {
echo "You've selected Theme Template #1.";
} elseif ( 'aa_theme_temp2' == $aa_mb_select_val ) {
echo "You've selected Theme Template #2.";
} else {
echo "You've selected Theme Template #3.";
}
?>
获取保存的值的过程与我在管理面板中讨论的过程几乎相同。 唯一的区别是第6行,该行通过get_the_ID()
函数检索特定页面或帖子ID的保存值。
在前端显示结果
让我们选择' Theme Template 3 '作为演示设置。 这是前端的屏幕截图。
在主题定制器部分中创建选择类型选项
声明示例
最后,我将在主题定制器部分中添加此选项。
<?php
/**
*
* Create select type option in a Theme Customizer Section
*
*/
$aa_section1->createOption( array(
'id' => 'aa_sec_select', // The ID which will be used to get the value of this option
'type' => 'select', // Type of option we are creating
'name' => 'My Select Option', // Name of the option which will be displayed
'desc' => 'This is our option', // Description of the option which will be displayed
'options' => array( //An array of value-label pairs which appears as options
'aa_red' => 'Red',
'aa_blue' => 'Blue',
'aa_green' => 'Green'
),
'default' => 'aa_blue'
) );
在这里,我在主题定制器部分$aa_section1
添加了select
type选项。
这就是在定制器中的外观。
在上面的屏幕快照中,您可以在名为My Section的主题定制器部分中找到一个包含颜色的下拉列表。
用法示例
这是代码。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_sec_select_val = $titan->getOption( 'aa_sec_select' );
/**
*
* Print Customizer option values
*
*/
?>
<div
style="color: <?php echo $aa_sec_select_val; ?>;">
<h1> My Site Header Text. </h2>
</div>
首先,我通过getInstance()
函数注册了一个实例,然后通过#3和#6行中的getOption()
函数注册了保存的值。 接下来,在第16行中,我创建了带有一些虚拟文本的div标签。 最后,我使用内联CSS来打印颜色值。
在前端显示结果
这是实时预览模式下发生的更改的屏幕截图。
在构建WordPress主题或插件时,有时我需要为最终用户提供一个选项,以附加具有某些自定义功能的特定帖子。 这就是Titan Framework的select-posts
类型选项派上用场的地方。
Titan Framework中的“选择职位”类型选项
在Titan框架中,您可以创建一个包含现有帖子列表的下拉菜单。 这可以通过select-posts
类型选项来实现。 还支持自定义帖子类型,我将在本文后面讨论。
让我们看一下它的参数:
- name :显示
select-posts
类型选项的名称。 - id :此参数分配一个唯一名称,该名称将获取保存的选项值。
- desc :添加简短说明。
- default :(可选)此参数配置默认的帖子ID。
- livepreview :(可选)在主题定制器中添加
select-posts
类型选项时,可以使用此参数预览实时更改。 - post_type :(可选)它指定出现在下拉列表中的帖子的类型。 默认设置为
post
。 - post_status :(可选)此参数控制显示在选项列表中的显示帖子的状态。 默认值设置为
any
,但是可以将其设置为publish,pending,draft等。 了解更多信息! - num :(可选)它指定下拉列表中的帖子数。 默认设置为
-1
,它引用所有帖子。 - orderby :(可选)列出的帖子的显示顺序由此参数控制。 默认设置按
post_date
列出它们。 您可以将其设置为作者,ID,标题,类型等。 了解更多! - order :(可选)此参数确定选项的显示顺序是升序(
asc
)还是降序(desc
)。 默认设置为desc
。
除了“ num” (本质上是int
之外,所有参数都是按类型排列的 string
。
选择帖子类型选项的可用容器
select-posts
类型选项添加在:
- 管理面板
- 管理员标签
- 元框
- 主题定制器部分
要添加此选项,您需要遵循统一的步骤模式:
- 通过
getInstance()
函数获取实例。 - 然后通过
createOption()
函数添加一个选项。 - 最后,通过
getOption()
函数检索保存的值。
如果您不知道如何在Titan Framework中创建容器,请阅读本系列以前的文章。
在管理面板中创建选择帖子类型选项
声明示例
首先,我将在管理面板中添加此选项。
<?php
/**
*
* Create the slect-posts type option in an Admin Panel
*
*/
$aa_panel->createOption( array(
'id' => 'aa_select_posts_opt', // The ID which will be used to get the value of this option
'type' => 'select-posts', // Type of option we are creating
'name' => 'Select Posts Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option' // Description of the option which will be displayed in the admin panel
) );
此代码通过createOption()
函数在管理面板$aa_panel
添加了一个select-posts
类型选项。 我使用的参数非常简单: id,name,type和desc 。 在这些参数中,ID应该始终是唯一的。
因此,将创建一个ID为aa_select_posts_opt
且名称为' Select Posts Option '的select-posts
选项,如下所示:
在上面的屏幕截图中,有一个名为Select Posts Option的下拉菜单,其中列出了所有帖子。 请注意,此选项位于管理面板的“ 整洁选项”中 。
用法示例
让我们检索保存的选项值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_panel_getpostid_val = $titan->getOption( 'aa_select_posts_opt' );
// 3. Get the post title from the saved post ID
$aa_panel_getpost_title_val = get_post_field('post_title', $aa_panel_getpostid_val);
/**
*
* Print Admin panel options
*
*/
?>
<div>
The title of the selected post is: <?php echo $aa_panel_getpost_title_val; ?>
</div>
在第3行中,首先通过getInstance()
函数获取实例; 我在这里使用主题名称作为参数。 然后在第6行中,通过getOption()
函数以ID aa_select_posts_opt
作为其参数检索保存的值,并将其保存到变量$aa_panel_getpostid_val
。
在第9行的下一步中,我使用了get_post_field()
函数,该函数根据帖子ID从帖子字段检索数据。 因此,帖子ID由变量$aa_panel_getpostid_val
,并与post_title
参数一起,将所选帖子的标题保存在新变量$aa_panel_getpost_title_val
。
然后,我在div (第20行)中使用echo
命令在前端打印了它。 很迷人,不是吗? 选定帖子ID后,您可以执行许多操作。
在前端显示结果
假设我选择Hello World作为演示设置。
前端将显示如下:
在“管理”标签中创建“选择帖子类型”选项
声明示例
让我们在管理标签中创建此选项。
<?php
/**
*
* Create the select-posts type option in an Admin Tab
*
*/
$aa_tab1->createOption( array(
'id' => 'aa_select_posts_opt_in_tab1', // The ID which will be used to get the value of this option
'type' => 'select-posts', // Type of option we are creating
'name' => 'Select Posts Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option', // Description of the option which will be displayed in the admin panel
'post_status' => 'draft' //Status of the displayed options
) );
现在,我将此选项添加到ID $aa_tab1
的管理选项卡$aa_tab1
aa_select_posts_opt_in_tab1
。 请注意,参数列表中有一个新添加项。 在第14行中,我使用了值为值draft
的' post_status'参数。 这意味着现在在select-posts
列表中仅显示草稿帖子。
在屏幕快照中,“ 整洁选项2 ”面板的选项 卡1内有一个名为“ 选择帖子选项”的列表。 您还会发现由于明显的原因,列表选项比以前少了。
用法示例
让我们得到它的价值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_tab_getpostid_val = $titan->getOption( 'aa_select_posts_opt_in_tab1' );
// 3. Get the post title from the saved post ID
$aa_tab_getpost_title_val = get_post_field('post_title', $aa_tab_getpostid_val);
/**
*
* Print Admin Tab options
*
*/
?>
<div>
The title of the selected draft post is: <?php echo $aa_tab_getpost_title_val; ?>
</div>
保存的过程几乎相同,所以让我简要总结所有要点:
- 在第3行中获得一个唯一实例。
- 通过第6行的
getOption()
函数获取保存的值。 - 通过第9行的
get_post_field()
函数获取草稿标题。 - 在第20行中使用
echo
命令打印变量$aa_tab_getpost_title_val
的结果。
在前端显示结果
这次我选择“ 投资组合 ”。 这是前端的屏幕截图。
在元框内创建选择帖子类型选项
声明示例
现在,我将在一个metabox中创建此选项。
<?php
/**
*
* Create the select-posts type option in a Meta box
*
*/
$aa_metbox->createOption( array(
'id' => 'aa_select_posts_opt_in_metabox', // The ID which will be used to get the value of this option
'type' => 'select-posts', // Type of option we are creating
'name' => 'Select Page Option', // Name of the option which will be displayed
'desc' => 'This is our option', // Description of the option which will be displayed
'post_type' => 'page' //Type of the displayed options
) );
现在,我将此选项添加到ID为aa_select_posts_opt_in_metabox
$aa_metbox
aa_select_posts_opt_in_metabox
。 我在第14行中使用了“ post_type”参数,其值设置为page
。 这意味着只有页面会在下拉菜单中列出。
上面的屏幕快照显示了页面编辑屏幕,并且在底部的右侧是一个“ 选择页面选项”,其中包含一个metabox中的页面列表。
用法示例
使用此代码。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_metabox_getpostid_val = $titan->getOption( 'aa_select_posts_opt_in_metabox', get_the_ID() );
// 3. Get the post title from the saved post ID
$aa_metabox_getpost_title_val = get_post_field('post_title', $aa_metabox_getpostid_val);
/**
*
* Print Metabox options
*
*/
?>
<div>
The title of the selected page in a metabox is: <?php echo $aa_metabox_getpost_title_val; ?>
</div>
整个代码与我之前使用的代码非常相似。 您只会在第6行中找到另一个参数,即get_the_ID()
函数。
在前端显示结果
假设我选择了“ Titan Framework”页面。 前端看起来像这样:
在主题定制器部分中创建选择帖子类型选项
声明示例
使用下面编写的代码。
<?php
/**
*
* Create the slect-posts type option in a Customizer Section
*
*/
$aa_section1->createOption( array(
'id' => 'aa_select_posts_opt_in_sec', // The ID which will be used to get the value of this option
'type' => 'select-posts', // Type of option we are creating
'name' => 'Select Post Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
最后,主题定制器部分$aa_section1
存在ID为aa_select_posts_opt_in_sec
的select-posts
类型选项。
该图像在名为My Section的定制程序部分中显示了此选项。
用法示例
现在,我将编写代码以获取保存的值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_sec_getpostid_val = $titan->getOption( 'aa_select_posts_opt_in_sec' );
// 3. Get the post title from the saved post ID
$aa_sec_getpost_title_val = get_post_field('post_title', $aa_sec_getpostid_val);
/**
*
* Print Customizer Section options
*
*/
?>
<div>
The title of the selected post in a theme customizer section is: <?php echo $aa_sec_getpost_title_val; ?>
</div>
再次,此代码是相同的。 此处仅ID和变量名称不同。
在前端显示结果
这是屏幕截图。
该选项实施起来非常简单。 如果希望特定的脚本,函数或操作/过滤器仅在访问所选帖子时运行,则可以为最终用户提供此选项。 确实,这里的可能性是无限的。
接下来,我们将讨论Titan Framework的select-pages
类型选项。 让我们看看如何为最终用户添加一个选项,以使其在自定义管理面板,元框或主题定制器中选择现有页面。
Titan Framework中的“选择页面类型”选项
与select-posts
相似,Titan Framework中存在一个select-pages
类型选项,该选项显示包含网站现有页面的下拉列表。 它在仪表盘中的显示如下:
让我们列出该选项支持的参数:
- name :此参数分配
select-pages
类型选项的显示名称。 - id :这是一个唯一名称,有助于检索保存的值。
- desc :添加一行描述。
- default :(可选)此参数分配默认页面ID。
- livepreview :(可选)在主题定制器中添加此选项时,可以使用此参数预览实时更改。
所有参数按类型都是string
。
选择页面类型选项的可用容器
您可以在以下位置添加此选项:
- 管理面板
- 管理员标签
- 元框
- 主题定制器部分
如果您需要了解有关使用Titan Framework添加容器的详细信息,请阅读本系列的前几篇文章。
- 通过
getInstance()
函数获取实例。 - 使用
createOption()
函数添加一个选项。 - 使用
getOption()
函数检索保存的值。
在管理面板中创建选择页面类型选项
声明示例
首先,我将在管理面板中添加此选项。
<?php
/**
*
* Create select-pages type option in an Admin Panel
*
*/
$aa_panel->createOption( array(
'id' => 'aa_select_pages_opt', // The ID which will be used to get the value of this option
'type' => 'select-pages', // Type of option we are creating
'name' => 'Select Pages Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option' // Description of the option which will be displayed in the admin panel
) );
在第8行中,我正在使用createOption()
函数在管理面板$aa_panel
添加select-pages
类型选项。 在所有参数中,我使用了name,id,type和desc 。
ID的值必须唯一,因为以后将用于获取保存的值。 在此,选项ID为aa_select_pages_opt
。
在屏幕快照中,在管理面板“ 整洁的选项”中以名称“ 选择页面选项 ”为标题显示了一个“ select-pages
类型” 选项 。 另外,查看列表条目。 这些是现有页面的名称。
用法示例
现在,我将获取保存的值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_getpageid_val = $titan->getOption( 'aa_select_pages_opt' );
// 3. Get the value of the page title via get_post_field function
$aa_panel_getpage_title_val = get_post_field('post_title', $aa_getpageid_val);
/**
*
* Printing saved values of Admin Panel
*
*/
?>
<div>
Title of selected page in an Admin Panel is: <?php echo $aa_panel_getpage_title_val; ?>
</div>
首先,通过第3行中的getInstance()
函数获取一个实例。 然后在第5行中,我使用的是getOption()
函数,该函数通过将选项ID aa_select_pages_opt
注册为参数来获取保存的值。 该函数的结果保存在变量$aa_panel_getpageid_val
。
在第7行中,有一个get_post_field()
函数,该函数可用于根据帖子ID从帖子字段获取数据。
变量$aa_panel_getpageid_val
提供所选页面的页面ID和post_title
参数,并检索标题,然后将其保存到新变量$aa_panel_getpage_title_val
。
最后,在第19行中,我通过div中的echo
命令在前端打印页面标题。
在前端显示结果
假设我从列表中选择“ 关于我们 ”页面并保存设置。
该页面的标题像这样在前端打印:
在管理标签内创建选择页面类型选项
声明示例
现在,我将在管理选项卡中添加此选项。
<?php
/**
*
* Create select-pages type option in an Admin Tab
*
*/
$aa_tab1->createOption( array(
'id' => 'aa_select_pages_opt_in_tab', // The ID which will be used to get the value of this option
'type' => 'select-pages', // Type of option we are creating
'name' => 'Select Pages Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option' // Description of the option which will be displayed in the admin panel
) );
这次,我在admin标签$aa_tab1
内添加了一个select-pages
选项。 其ID为aa_select_pages_opt_in_tab
。 其余参数相同。
在图像中,您可以找到一个下拉列表,其中包含管理面板Neat Options 2的 选项卡1内的页面。
用法示例
让我们检索保存的选项值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_tab_getpageid_val = $titan->getOption( 'aa_select_pages_opt_in_tab' );
// 3. Get the value of the page title via get_post_field function
$aa_tab_getpage_title_val = get_post_field('post_title', $aa_tab_getpageid_val);
/**
*
* Printing saved values of Admin Tab
*
*/
?>
<div>
Title of selected page in an Admin Tab is: <?php echo $aa_tab_getpage_title_val; ?>
</div>
可以通过执行相同的步骤来检索“管理”选项卡的值:
- 在第3行中获得一个唯一实例。
- 在
getOption()
函数(第5行getOption()
注册选项IDaa_select_pages_opt_in_tab
。 - 将结果保存在变量
$aa_tab_getpageid_val
。 - 通过第7行的
get_post_field()
函数获取所选页面的标题。 - 最后,通过第17行中的
echo
命令在前端打印页面标题。
在前端显示结果
这次,我选择了“ 联系人 ”页面,其前端的标题如下所示:
在元框内创建选择页面类型选项
声明示例
让我们在一个metabox中创建一个select-pages
类型选项。
<?php
/**
*
* Create select-pages type option in a Metabox
*
*/
$aa_metbox->createOption( array(
'id' => 'aa_select_pages_opt_in_metabox', // The ID which will be used to get the value of this option
'type' => 'select-pages', // Type of option we are creating
'name' => 'Select Pages Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
我在metabox $aa_metbox
内创建了一个ID为aa_select_pages_opt_in_metabox
的select-pages
类型选项。
在上面的屏幕截图中,您可以在页面编辑屏幕的末尾找到一个metabox。 内部有一个“ 选择页面”选项 ,其中列出了所有页面。
用法示例
使用此代码获取值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_metabox_getpageid_val = $titan->getOption( 'aa_select_pages_opt_in_metabox', get_the_ID() );
// 3. Get the value of the page title via get_post_field function
$aa_metabox_getpage_title_val = get_post_field('post_title', $aa_metabox_getpageid_val);
/**
*
* Printing saved values of Metabox
*
*/
?>
<div>
Title of selected page in a Metabox is: <?php echo $aa_metabox_getpage_title_val; ?>
</div>
在这段代码中,我在getOption()
函数(第5行getOption()
注册了一个附加参数。 这是get_the_ID()
函数。 其余代码几乎相同。
在前端显示结果
现在假设我选择“ Sample Page ”。 前端将打印相应的标题,如下所示:
在主题定制器部分中创建选择页面类型选项
声明示例
最后,我将在自定义程序部分中添加此选项。
<?php
/**
*
* Create select-pages type option in a Theme Customizer Section
*
*/
$aa_section1->createOption( array(
'id' => 'aa_select_pages_opt_in_sec', // The ID which will be used to get the value of this option
'type' => 'select-pages', // Type of option we are creating
'name' => 'Select Pages Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
我在名为$aa_section1
的主题定制程序部分中创建了一个ID为aa_select_pages_opt_in_sec
的select-pages
类型选项。
用法示例
让我们获取保存的值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_sec_getpageid_val = $titan->getOption( 'aa_select_pages_opt_in_sec' );
// 3. Get the value of the page title via get_post_field function
$aa_sec_getpage_title_val = get_post_field('post_title', $aa_sec_getpageid_val);
/**
*
* Printing saved values of Customizer Section
*
*/
?>
<div>
Title of selected page in a Customizer Section is: <?php echo $aa_sec_getpage_title_val; ?>
</div>
在前端显示结果
这是在实时模式下预览的更改的屏幕截图。
考虑一种情况,您需要最终用户为特色帖子选择类别。 您需要一个下拉选择选项,其中包含WordPress类别作为选项。 Titan Framework可以帮助您使用称为select-categories
的选项类型创建此类选项。 让我们看看如何使用它。
Titan框架中的“选择类别”类型选项
在Titan框架中, select-categories
类型选项创建一个包含所有现有帖子类别的下拉菜单。 此选项也支持其他分类法。
此选项定义参数列表:
- name :它指定选项的名称。
- id :此参数分配一个唯一名称,该名称将获取保存的值。
- desc :添加带有选项名称的简短描述。
- default :(可选)此参数设置默认类别ID。
- livepreview :(可选)每当您在主题定制器部分中添加
select-categories
选项时,此参数都可让您预览所做的实时更改。 - orderby :(可选)决定要显示的顺序类别。 默认设置为
name
。 您可以相应地更改它。 - order :(可选)此参数以升序或降序列出类别。 默认设置为
asc
。 - 分类法 :(可选)它有助于显示要显示的分类法类型。 其默认值为
category
。 - hide_empty :(可选)如果此参数设置为
true
,则不会显示所有没有帖子的此类类别和/或分类法。 默认值为false
。 - show_count :(可选)如果设置为
true
则此参数显示每个类别中的帖子数。 默认设置为false
。
最后两个参数hide_empty和show_count是boolean
,其余参数按类型是string
。
选择类别类型选项的可用容器
让我们列出可以在其中添加此选项的容器:
- 管理面板
- 管理员标签
- 元框
- 主题定制器部分
不管容器类型如何,都可以通过以下步骤添加select-categories
选项:
- 首先通过
getInstance()
函数获取一个实例。 - 接下来,通过
createOption()
函数添加一个选项。 - 然后通过
getOption()
函数获取保存的选项值。
在管理面板中创建选择类别类型选项
声明示例
首先,让我们在管理面板中创建此选项。
<?php
/**
*
* Create select-categories type option in an Admin Panel
*
*/
$aa_panel->createOption( array(
'id' => 'aa_select_categories_opt', // The ID which will be used to get the value of this option
'type' => 'select-categories', // Type of option we are creating
'name' => 'Select Categories Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option' // Description of the option which will be displayed in the admin panel
) );
在第8行, createOption()
函数在名为$aa_panel
的管理面板中添加了一个select-categories
类型的选项。 我定义了一些参数,即name,type,id和desc 。 ID的值应始终唯一,在第10 aa_select_categories_opt
。
在上面的屏幕截图中,有一个“ 整洁的选项”面板,其中有一个显示所有帖子类别的下拉列表。
用法示例
现在,我将获取保存的选项值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_panel_getcategoryid_val = $titan->getOption( 'aa_select_categories_opt' );
// 3. Get title of the selected category
$aa_panel_getcategory_title_val = get_cat_name( $aa_panel_getcategoryid_val);
/**
*
* Print Saved Values in an Admin Panel
*
*/
?>
<div>
Title of the selected category in an Admin Panel is: <?php echo $aa_panel_getcategory_title_val; ?>
</div>
在第3行,我使用的是getInstance()
函数,该函数占用一个唯一的参数,最好是您的主题名称。 然后,我在第6行使用getOption()
函数通过注册选项ID(即aa_select_categories_opt
来检索保存的值。 结果保存在变量$aa_panel_getcategoryid_val
。 到目前为止,所选类别的ID放置在此变量内。
假设我要在前端显示类别的标题。 为了实现这一点,我将使用get_cat_name()
函数。 看#9行,它从变量$aa_panel_getcategoryid_val
检索类别标题。
我将标题保存在变量$aa_panel_getcategory_title_val
,然后回显它。
在前端显示结果
假设我从列表中选择“ 投资组合 ”类别并保存设置。
因此,根据代码,“ 管理面板中所选类别的标题为:投资组合 ”必须在前端打印。
在管理标签内创建选择类别类型选项
声明示例
接下来,我将其添加到“管理”标签中。
<?php
/**
*
* Create select-categories type option in an Admin Tab
*
*/
$aa_tab1->createOption( array(
'id' => 'aa_select_categories_opt_in_tab', // The ID which will be used to get the value of this option
'type' => 'select-categories', // Type of option we are creating
'name' => 'Select Categories Option', // Name of the option which will be displayed in the admin panel
'desc' => 'This is our option', // Description of the option which will be displayed in the admin panel
'order' => 'desc', // Display order of the categories
'show_count' => 'true' // Display no. of posts with each category
) );
在这里,我在admin标签$aa_tab1
添加了select-categories
type选项。 它的唯一ID是aa_select_categories_opt_in_tab
。 看一下参数列表。 您会发现一些新添加的内容,即order和show_count参数。 它们的功能已在前面讨论过。
在屏幕快照中,您可以在Neat Options 2面板的Tab 1中找到名为Select Categories Option的选项 。 请注意,由于有show_count参数,因此也会显示每个类别中的帖子数。 类别的显示顺序也与以前不同。
用法示例
让我们检索保存的值。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_tab_getcategoryid_val = $titan->getOption( 'aa_select_categories_in_tab' );
// 3. Get title of the selected category
$aa_tab_getcategory_title_val = get_cat_name( $aa_tab_getcategoryid_val);
/**
*
* Print Saved Values in an Admin Tab
*
*/
?>
<div>
Title of the selected category in an Admin Tab is: <?php echo $aa_tab_getcategory_title_val; ?>
</div>
这段代码非常相似。 这是步骤的摘要:
- 在第3行上获取一个实例。
- 在第6行使用
getOption()
函数通过ID检索保存的值。 - 通过第9行的
get_cat_name()
函数获取所选类别的标题。 - 通过#22行上的
echo
命令在前端打印类别标题。
在前端显示结果
我选择“ 传记 ”作为演示值。 前端如下所示:
在元框内创建选择类别类型选项
声明示例
现在,我将在metabox中添加一个select-categories
类型选项。
<?php
/**
*
* Create select-categories type option in a Metabox
*
*/
$aa_metbox->createOption( array(
'id' => 'aa_select_categories_opt_in_metabox', // The ID which will be used to get the value of this option
'type' => 'select-categories', // Type of option we are creating
'name' => 'Select Categories Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
我在ID为aa_select_categories_opt_in_metabox
$aa_metbox
创建了一个select-categories
类型选项。
在上图中,您可以在页面编辑屏幕的末尾找到一个metabox。 内部是一个包含所有帖子类别的下拉列表。
用法示例
使用以下代码。
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_metabox_getcategoryid_val = $titan->getOption( 'aa_select_categories_in_metabox', get_the_ID() );
// 3. Get title of the selected category
$aa_metabox_getcategory_title_val = get_cat_name( $aa_metabox_getcategoryid_val);
/**
*
* Print Saved Values in a Metabox
*
*/
?>
<div>
Title of the selected category in a Metabox is: <?php echo $aa_metabox_getcategory_title_val; ?>
</div>
在第6行上,我已将get_the_ID()
函数用作getOption()
函数中的参数。 此附加参数用于通过特定页面或帖子ID获取保存的值。 其余代码相同。
在前端显示结果
这次我选择“ 故事 ”,结果显示如下:
在主题定制器部分中创建选择类别类型选项
声明示例
最后,我将在主题定制器部分中创建此选项。
<?php
/**
*
* Create select-categories type option in a Customizer Section
*
*/
$aa_section1->createOption( array(
'id' => 'aa_select_categories_opt_in_sec', // The ID which will be used to get the value of this option
'type' => 'select-categories', // Type of option we are creating
'name' => 'Select Categories Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
我将此选项添加到ID $aa_section1
的主题定制器部分$aa_section1
aa_select_categories_opt_in_sec
。
在此图像中,有一个我的部分 定制程序部分,其中包含定制程序内类别的下拉列表。
用法示例
使用以下代码获取值:
<?php
// 1. Get the titan framework instance and save it to a variable
$titan = TitanFramework::getInstance( 'neat' ); // we will initialize $titan only once for every file where we use it.
// 2. Get the value via ID using getOption function
$aa_sec_getcategoryid_val = $titan->getOption( 'aa_select_categories_in_sec' );
// 3. Get title of the selected category
$aa_sec_getcategory_title_val = get_cat_name( $aa_sec_getcategoryid_val);
/**
*
* Print Saved Values in a Theme Customizer Section
*
*/
?>
<div>
Title of the selected category in a Customizer Section is: <?php echo $aa_sec_getcategory_title_val; ?>
</div>
此代码与上面的代码几乎相同。 仅ID和变量名称不同。
在前端显示结果
这是实时预览模式下见证的更改的屏幕截图。
结论
至此,我们已经讨论了与Titan框架中的select
选项相关的所有选项类型。
例如,如果我要编写一个为最终用户提供15个布局元素的插件,我可以以选择框的形式提供这些元素,以保持选项的简洁和最小化。
让我知道您是否对此选项有创造力的用户案例。 您可以在Twitter上与我联系,也可以在下面的框中留下评论。