以前,我们了解了如何允许用户使用Titan Framework中的multicheck
type选项启用多个选择。 现在,我们将讨论另一个选项,该选项可与多重检查结合使用,但只能用于现有帖子。 这是multicheck-posts
选项。 让我们探索它的功能,并找到如何使用它。
Titan Framework中的Multicheck-Posts类型选项
Titan Framework使得可以通过复选框将您现有的Web帖子显示为选项。 这是通过创建multicheck-posts
类型选项来实现的。 使用此选项,您可以一次启用多个复选框。 还支持自定义帖子类型。 在您的信息中心中,它看起来像这样:
让我们列出该选项支持的参数设置:
- name :显示
multicheck-posts
类型选项的名称。 - id :此参数分配一个唯一名称,该名称检索选项值。
- desc :添加一行描述。
- default :(可选)此参数定义默认检查的帖子ID的数组。
- post_type :(可选)它控制在选项列表中显示为复选框的帖子的类型。 默认设置为
post
。 - post_status :(可选)此参数控制在选项列表中显示的帖子的状态。 默认值设置为
any
,但是可以将其设置为publish,pending,draft等。 了解更多信息! - num :(可选)它控制要显示在选择框中的帖子数。 默认设置为
-1
,表示所有帖子。 - orderby :(可选)列出的帖子的显示顺序由此参数控制。 默认设置按
post_date
列出它们。 您可以将其设置为作者,ID,标题,类型等。 了解更多! - order :(可选)此参数确定选项的显示顺序是升序(
asc
)还是降序(desc
)。 默认设置为desc
。
参数num是int
,其余参数按类型是string
。
Multicheck-Posts类型选项的可用容器
您可以在以下位置创建此选项:
- 管理面板
- 管理员标签
- 元框
- 主题定制器部分
这些容器是在Titan框架中创建的。 想学习如何? 浏览本系列的前几篇文章。
因此,让我列出在每个容器中添加此选项的步骤:
- 通过
getInstance()
函数获取实例。 - 通过
createOption()
函数添加一个选项。 - 通过
getOption()
函数获取保存的值。
在管理面板中创建Multicheck-Posts Type选项
声明示例
首先,让我们在管理面板中创建此选项。
<?php
/**
*
* Create multicheck-posts type option in an Admin Panel
*
*/
$aa_panel->createOption( array(
'id' => 'aa_multicheck_posts_opt', // The ID which will be used to get the value of this option
'type' => 'multicheck-posts', // Type of option we are creating
'name' => 'Multicheck 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
) );
第8行的createOption()
函数在自定义管理面板$aa_panel
添加了multicheck-posts
类型选项。 该函数占用一个数组,是我定义了一些参数的地方。 这些参数( 名称,id,类型和desc )添加了此选项的最基本形式。
在本文的后面,我还将添加一些复杂的参数。
在上面的屏幕截图中,有一个自定义的管理面板页面“ 整洁的选项” ,其中有一个复选框列表,显示WordPress的所有最新帖子。
用法示例
让我们从此选项获取值。
<?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_getpostid_array = $titan->getOption( 'aa_multicheck_posts_opt' );
/**
*
* Printing array contents
*
*/
var_dump( $aa_getpostid_array );
?>
首先,通过getInstance()
函数在第3行中获得一个唯一实例。 然后在第6行中使用getOption()
函数。 将选项ID aa_multicheck_posts_opt
注册为参数,并将其保存在变量中,例如$aa_getpostid_array
。
当用户启用多个选项时,这意味着在变量$aa_getpostid_array
中存在一个数组,该数组包含用户选择的帖子ID。 因此,首先我们将通过第15行的var_dump()
命令打印其内容来检查数组的存在。 作为测试用例,我将启用所有五个选项。
上面的屏幕截图清楚地显示了一个包含五个条目的数组,每个条目包含帖子ID及其长度,例如,该数组的第一个元素的“ post ID = 9 ”。
第二种情况,让我们分别打印选定的帖子ID。
<?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_getpostid_array = $titan->getOption( 'aa_multicheck_posts_opt' );
/**
* Printing individual array elements
*
*/
echo $aa_getpostid_array[0];
echo $aa_getpostid_array[1];
?>
该代码的第一部分完全相同。 打印帖子ID的行是不同的。 第15和16行使用echo
命令,每个命令都包含数组元素'0'和'1'的变量。
屏幕快照显示了提到的数组元素的ID。 来自第一个元素的帖子的ID为9,而来自数组第二个元素的ID为11。
顺便说一句,我的意图不是让它看起来好像我在这里拨打911!
现在让我们采用一种更专业的方法,在该方法中,将打印所选帖子的标题。
<?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_getpostid_array = $titan->getOption( 'aa_multicheck_posts_opt' );
// Get selected post titles by array ID
$aa_panel_getpost_title0_val = get_post_field('post_title', $aa_getpostid_array[0]);
$aa_panel_getpost_title1_val = get_post_field('post_title', $aa_getpostid_array[1]);
/**
*
* Printing selected post titles
*
*/
echo $aa_panel_getpost_title0_val;
echo $aa_panel_getpost_title1_val;
?>
我添加了一个实例,然后保存了选项值。 在第6行中,变量$aa_getpostid_array
包含一个帖子ID数组。 现在要检索标题,我在第9行中使用get_post_field()
函数,该函数带有参数post_title
和$aa_getpostid_array[0]
。 这意味着第一个数组元素的帖子标题将被获取并保存在新变量$aa_panel_getpost_title0_val
。
相同的解释适用于第10行,除了它获得数组第二个元素的帖子标题,即$aa_getpostid_array[1]
。
到目前为止,所选帖子标题的标题保存在变量$aa_panel_getpost_title0_val
和$aa_panel_getpost_title1_val
。 为了在前端打印这些,我使用了echo
命令(第19和20行)。
在前端显示结果
假设我选择以下职位。
这是数组的第一个和第二个元素的帖子标题的屏幕快照(我没有添加任何<br/>
,所以这就是为什么这些标题出现在同一行)。
在“管理”标签中创建Multicheck-Posts类型选项
声明示例
接下来,在管理标签中创建multicheck-posts
选项。
<?php
/**
*
* Create multicheck-posts type option in an Admin Tab
*
*/
$aa_tab1->createOption( array(
'id' => 'aa_multicheck_posts_opt_in_tab', // The ID which will be used to get the value of this option
'type' => 'multicheck-posts', // Type of option we are creating
'name' => 'Multicheck Posts Option', // Name of the option which will be displayed in the admin panel
'desc' => 'Posts which are pending for review.', // Description of the option which will be displayed in the admin panel
'post_status' => 'pending' //Status of the displayed options
) );
现在,我已在管理员选项卡此选项$aa_tab1
与ID aa_multicheck_posts_opt_in_tab1
。 您会看到参数列表中有一个新添加项(第14行)。 这次,我使用了值为pending
的' post_status'参数。 现在,选项列表将显示待审核的帖子。
在上面的屏幕截图中,您将找到两个主要更改。 首先, multicheck-posts
type选项现在位于Neat Options 2面板的Tab 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_array = $titan->getOption( 'aa_multicheck_posts_opt_in_tab' );
// Get selected post titles by array ID
$aa_tab_getpost_title0_val = get_post_field('post_title', $aa_tab_getpostid_array[0]);
$aa_tab_getpost_title1_val = get_post_field('post_title', $aa_tab_getpostid_array[1]);
$aa_tab_getpost_title2_val = get_post_field('post_title', $aa_tab_getpostid_array[2]);
/**
*
* Printing selected post titles
*
*/
echo $aa_tab_getpost_title0_val;
echo $aa_tab_getpost_title1_val;
echo $aa_tab_getpost_title2_val;
?>
此代码与我在管理面板中编写的代码非常相似。 不同之处在于,现在我在前端为数组的前三个元素打印了标题。
在前端显示结果
根据代码,我应该从仪表板启用所有三个发布选项,如下所示:
标题是这样打印在前端的(再次,我没有添加任何<br/>
元素以避免代码中的混乱)。
在元框内创建Multicheck-Posts类型选项
声明示例
接下来,我们将在metabox中添加multicheck-posts
类型选项。
<?php
/**
*
* Create multicheck-posts type option in a Metabox
*
*/
$aa_metbox->createOption( array(
'id' => 'aa_multicheck_posts_opt_in_metabox', // The ID which will be used to get the value of this option
'type' => 'multicheck-posts', // Type of option we are creating
'name' => 'Multicheck Posts Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
$aa_metbox
添加了ID为aa_multicheck_posts_opt_in_metabox
multicheck-posts
类型选项。 它出现在所有页面和后期编辑屏幕上。
此图显示了页面编辑屏幕,并且在结尾处有一个名为Meta Box Options的metabox。 在其中,有一个显示为复选框的Web帖子列表。
用法示例
让我们获取保存的值。
<?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_array = $titan->getOption( 'aa_multicheck_posts_opt_in_metabox', get_the_ID() );
// Get selected post titles by array ID
$aa_metabox_getpost_title0_val = get_post_field('post_title', $aa_metabox_getpostid_array[0]);
/**
*
* Printing selected post titles
*
*/
echo $aa_metabox_getpost_title0_val;
?>
这次,我编写了仅打印第一个数组元素的帖子标题的代码。 该例程与第6行相同,只是在第6行中,您会发现在getOption()
函数内注册了两个参数。 一个是选项ID aa_multicheck_posts_opt_in_metabox
,另一个是get_the_ID()
函数,该函数获取特定页面或帖子ID的ID值。
在前端显示结果
第零个数组元素的标题显示如下:
在主题定制器部分中创建Multicheck-Posts类型选项
声明示例
最后,我们将在主题定制器部分中添加multicheck-posts
类型选项。
<?php
/**
*
* Create multicheck-posts type option in a Theme Customizer Section
*
*/
$aa_section1->createOption( array(
'id' => 'aa_multicheck_posts_opt_in_sec', // The ID which will be used to get the value of this option
'type' => 'multicheck-posts', // Type of option we are creating
'name' => 'Multicheck Posts Option', // Name of the option which will be displayed
'desc' => 'This is our option' // Description of the option which will be displayed
) );
因此,我在主题定制器部分$aa_section1
添加了multicheck-posts
类型选项。 它的唯一ID是aa_multicheck_posts_opt_in_sec
。
在上面的屏幕截图中,您可以在定制器部分My Section中找到名为Multicheck 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_sec_getpostid_array = $titan->getOption( 'aa_multicheck_posts_opt_in_sec' );
// Get selected post titles by array ID
$aa_sec_getpost_title0_val = get_post_field('post_title', $aa_sec_getpostid_array[0]);
$aa_sec_getpost_title1_val = get_post_field('post_title', $aa_sec_getpostid_array[1]);
$aa_sec_getpost_title2_val = get_post_field('post_title', $aa_sec_getpostid_array[2]);
$aa_sec_getpost_title3_val = get_post_field('post_title', $aa_sec_getpostid_array[3]);
$aa_sec_getpost_title4_val = get_post_field('post_title', $aa_sec_getpostid_array[4]);
/**
*
* Printing selected post titles
*
*/
echo $aa_sec_getpost_title0_val;
echo $aa_sec_getpost_title1_val;
echo $aa_sec_getpost_title2_val;
echo $aa_sec_getpost_title3_val;
echo $aa_sec_getpost_title4_val;
?>
在最后一个容器中,我将打印整个数组的标题,即所有五个发布选项。 详细信息相同-仅ID和变量名称不同。
在前端显示结果
这是在实时预览模式下观察到的更改的屏幕截图。
我们将通过讨论Titan Framework中的multicheck-pages
type选项来继续本文,该选项的功能与multicheck-posts非常相似。 让我们看看如何在自定义管理面板,元框或自定义程序中创建可用WordPress页面的多重检查列表。
Titan框架中的Multicheck-Pages类型选项
使用Titan Framework中的multicheck-pages
type选项,您可以显示所有现有页面的一组复选框。 看起来是这样的:
此参数支持一些设置参数:
- name :它分配此选项的名称。
- id :此参数指定一个唯一名称,该名称将获取保存的选项值。
- desc :添加带有选项名称的简短描述。
- default :(可选)此参数是一个数组,其中包含默认情况下已启用选项的页面ID。
参数' default'是按类型array
,其余参数本质上是string
。
Multicheck-Pages类型选项的可用容器
可以在以下位置添加此选项:
- 管理面板
- 管理员标签
- 元框
- 主题定制器部分
在这些容器中的任何一个中创建multicheck-pages
类型选项时,请执行以下步骤。
- 首先通过
getInstance()
函数获取一个实例。 - 然后通过
createOption()
函数添加一个选项。 - 最后,通过
getOption()
函数获取保存的值。
如果您不知道如何使用Titan Framework创建这些容器,则可以参考本系列的前几篇文章。 您会找到所有相关的详细信息。
在管理面板中创建Multicheck-Pages类型选项
声明示例
首先,让我们在管理面板中创建此选项。
<?php
/**
*
* Create multicheck-pages type option in an Admin Panel
*
*/
$aa_panel->createOption( array(
'id' => 'aa_multicheck_pages_opt', // The ID which will be used to get the value of this option
'type' => 'multicheck-pages', // Type of option we are creating
'name' => 'Multicheck 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
添加createOption()
multicheck-pages
类型选项。 它包含一个参数数组: name,id,type和desc 。 所有这些,ID(即 aa_multicheck_pages_opt
)非常重要,因为它将用于获取保存的选项的值。 您应该始终为ID添加唯一值。
上图显示了管理面板Neat Options中名为Multicheck Pages 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_getpageid_array = $titan->getOption( 'aa_multicheck_pages_opt' );
/**
*
* Printing array contents
*
*/
var_dump( $aa_getpageid_array );
?>
在第3行中,我通过getInstance()
函数添加了一个唯一实例。 最好将主题名称注册为参数(这就是为什么我在这里使用整洁的原因)。 接下来,我在第6行中使用了getOption()
函数来检索保存的值,并将选项ID aa_multicheck_pages_opt
注册为其参数。 最后将其分配给新变量$aa_getpageid_array
。
每次用户做出多项选择时,由页面ID组成的数组都会保存在变量$aa_getpageid_array
。
要打印值,首先我们将测试是否存在数组。 为此,我将启用仪表板上的所有复选框,并通过var_dump()
命令(第15行var_dump()
打印阵列内容。
在上图中,您可以看到已打印阵列内容。 它由四个元素(0到3)组成,每个元素包含页面ID及其长度。 例如,第一个元素的“ 页面ID = 19 ”。 因此,验证了阵列的存在。
到目前为止,此编码例程仅是一项安全测试,可避免许多将来的错误。 让我们进入下一个级别,在那里我将打印选定的页面ID。
<?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_array = $titan->getOption( 'aa_multicheck_pages_opt' );
/**
*
* Printing individual array elements
*
*/
?>
<div>
Page ID of zero'th array element is : <?php echo $aa_getpageid_array[0]; ?>
</div>
<div>
Page ID of one'th array element is : <?php echo $aa_getpageid_array[1]; ?>
</div>
为了打印单独的页面ID,我在div中分别回显了所需数组元素的变量(第16-23行)。 因此, $aa_getpageid_array[0]
打印第一个元素的页面ID, $aa_getpageid_array[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_getpageid_array = $titan->getOption( 'aa_multicheck_pages_opt' );
// Get selected page titles by array ID
$aa_panel_getpage_title0_val = get_post_field('post_title', $aa_getpageid_array[0]);
$aa_panel_getpage_title1_val = get_post_field('post_title', $aa_getpageid_array[1]);
/**
*
* Printing selected page titles
*
*/
?>
<div>
Page Title of zero'th array element is : <?php echo $aa_panel_getpage_title0_val; ?>
</div>
<div>
Page Title of one'th array element is : <?php echo $aa_panel_getpage_title1_val; ?>
</div>
这次,我检索了所选页面的标题并将其打印在前端。 在第9行,页面标题是通过get_post_field()
函数获取的。 该函数带有两个参数: post_title
和$aa_getpageid_array[0]
。 因此,将检索第一个数组元素的页面标题并将其保存在变量$aa_panel_getpage_title0_val
。 类似地,在第10行,我在这里检索了第二个数组元素的页面标题(即$aa_getpageid_array[1]
)。
为了在前端打印保存的页面标题,我创建了两个div (第21至28行),它们分别打印变量$aa_panel_getpost_title0_val
和$aa_panel_getpost_title1_val
。
在前端显示结果
假设我启用以下页面。
这是第一和第二数组元素的页面标题的屏幕快照。
在管理标签内创建Multicheck-Pages类型选项
声明示例
接下来,我将在管理标签中添加此选项。
<?php
/**
*
* Create multicheck-pages type option in an Admin Tab
*
*/
$aa_tab1->createOption( array(
'id' => 'aa_multicheck_pages_opt_in_tab', // The ID which will be used to get the value of this option
'type' => 'multicheck-pages', // Type of option we are creating
'name' => 'Multicheck 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
) );
现在,我在管理选项卡$aa_tab1
添加了此选项。 除了ID以外,所有参数都相同,即aa_multicheck_pages_opt_in_tab
。
在上面的屏幕截图中,面板Neat Options 2的选项卡1中存在一个multicheck-pages
type选项。
用法示例
让我们获取保存的值。
<?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_array = $titan->getOption( 'aa_multicheck_pages_opt_in_tab' );
// Get selected page titles by array ID
$aa_tab_getpage_title0_val = get_post_field('post_title', $aa_tab_getpageid_array[0]);
$aa_tab_getpage_title1_val = get_post_field('post_title', $aa_tab_getpageid_array[1]);
$aa_tab_getpage_title2_val = get_post_field('post_title', $aa_tab_getpageid_array[2]);
/**
*
* Printing selected page titles
*
*/
?>
<div>
Page Title of zero'th array element is : <?php echo $aa_tab_getpage_title0_val; ?>
</div>
<div>
Page Title of one'th array element is : <?php echo $aa_tab_getpage_title1_val; ?>
</div>
<div>
Page Title of two'th array element is : <?php echo $aa_tab_getpage_title2_val; ?>
</div>
在第3至6行中,我检索了一个实例并保存了选项值。 然后,我检索了前三个数组元素(第9至11行)的选定页面标题。 最后,我通过divs标签内的echo
命令在前端打印了标题。
我使代码简单易懂,以帮助您了解事物的工作原理,但是不建议您在未验证数组元素的情况下打印数组元素。
在前端显示结果
我从仪表板启用了前三个不同的页面选项,如下所示:
前端必须显示如下标题:
在元框内创建Multicheck-Pages类型选项
声明示例
让我们在metabox中添加multicheck-pages
类型选项。
<?php
/**
*
* Create multicheck-pages type option in a Metabox
*
*/
$aa_metbox->createOption( array(
'id' => 'aa_multicheck_pages_opt_in_metabox', // The ID which will be used to get the value of this option
'type' => 'multicheck-pages', // Type of option we are creating
'name' => 'Multicheck 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_multicheck_pages_opt_in_metabox
multicheck-pages
类型选项。
在图像中,您可以看到Multicheck Pages选项 在名为Metabox Options的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_array = $titan->getOption( 'aa_multicheck_pages_opt_in_metabox', get_the_ID() );
// Get selected page titles by array ID
$aa_metabox_getpage_title0_val = get_post_field('post_title', $aa_metabox_getpageid_array[0]);
/**
*
* Printing selected page titles
*
*/
?>
<div>
Page Title of zero'th array element is : <?php echo $aa_metabox_getpage_title0_val; ?>
</div>
此代码仅打印第零个数组元素的页面标题。 除了在第6行中可以找到在getOption()
函数内注册的新参数之外,所有步骤都相同。 这是get_the_ID()
函数,该函数获取特定页面或帖子ID的值。
在前端显示结果
第零个数组元素的页面标题显示如下:
在主题定制器部分中创建Multicheck-Pages类型选项
声明示例
最后,我将在定制程序部分添加一个multicheck-pages
类型选项。
<?php
/**
*
* Create multicheck-pages type option in a Theme Customizer Section
*
*/
$aa_section1->createOption( array(
'id' => 'aa_multicheck_pages_opt_in_sec', // The ID which will be used to get the value of this option
'type' => 'multicheck-pages', // Type of option we are creating
'name' => 'Multicheck 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
内创建一个multicheck-pages
类型选项。 它的唯一ID是aa_multicheck_pages_opt_in_sec
。
该图像显示了名为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_getpageid_array = $titan->getOption( 'aa_multicheck_pages_opt_in_sec' );
// Get selected page titles by array ID
$aa_sec_getpage_title0_val = get_post_field('post_title', $aa_sec_getpageid_array[0]);
$aa_sec_getpage_title1_val = get_post_field('post_title', $aa_sec_getpageid_array[1]);
$aa_sec_getpage_title2_val = get_post_field('post_title', $aa_sec_getpageid_array[2]);
$aa_sec_getpage_title3_val = get_post_field('post_title', $aa_sec_getpageid_array[3]);
/**
*
* Printing selected page titles
*
*/
?>
<div>
Page Title of 0'th array element is : <?php echo $aa_sec_getpage_title0_val; ?>
</div>
<div>
Page Title of 1'th array element is : <?php echo $aa_sec_getpage_title1_val; ?>
</div>
<div>
Page Title of 2'th array element is : <?php echo $aa_sec_getpage_title2_val; ?>
</div>
<div>
Page Title of 3'th array element is : <?php echo $aa_sec_getpage_title3_val; ?>
</div>
这次我打印了所有现有页面的标题。 整个代码几乎相同。 仅ID和变量名称不同。
在前端显示结果
这是实时预览模式中更改的屏幕截图。
结论
您可以通过多种方式来实现此选项。 例如,如果您希望最终用户完全控制应将哪个页面显示为特定用户角色,以及应该限制哪个页面,该怎么办?
最后,让我们讨论一个与Titan框架中的multicheck type选项直接相关的选项类型。 它允许您通过复选框显示所有现有类别。 我说的是multicheck-categories
选项。 让我们学习该选项在自定义管理面板,元框和主题定制器部分中如何工作。
在Titan框架中,您可以为所有现有类别创建并启用多个复选框。 使用multicheck-categories
type选项可以做到这一点。 它也支持不同的分类法。 在仪表板上,它看起来像这样:
让我们列出该选项支持的参数:
最后两个参数hide_empty和show_count是boolean
, 默认是array
,其余是按类型array
的string
。
让我们列出可以在其中添加此选项的容器:
不管容器类型如何,都可以通过以下步骤添加multicheck-categories
选项:
首先,让我们在管理面板中创建此选项。
在第8行, createOption()
函数在管理面板$aa_panel
添加了multicheck-categories
类型选项。 在此函数内部,我定义了此选项支持的一些基本参数。 这些参数是name,id,type和desc 。 ID的值(即aa_multicheck_categories_opt
)应始终是唯一的。
该图像在管理面板“ 整洁的选项”中显示了此选项 。
让我们编写代码以获取保存的选项值。
由于此选项允许最终用户启用多个类别,因此它将返回一个类别ID数组。 为了打印该数组,我在第3行通过getInstance()
函数获得了一个实例。 然后,我在第6行中使用了getOption()
函数来获取保存的值。 此函数将ID aa_multicheck_categories_opt
注册为其参数,并将结果保存在变量$aa_getcategoryid_array
。 最后,我们将通过var_dump()
命令打印其内容。
理想的情况是启用所有复选框并保存设置。
这就是数组打印的方式。
上面的屏幕快照显示了一个由五个元素组成的数组。 每个元素都显示类别ID及其长度(不相关)。 例如,第零个数组元素具有“ 类别ID = 4 ”。
让我们现在打印类别名称。
在第3至6行中,我注册了一个实例并检索了选项的值。 至此,所选类别的ID保存在数组$aa_getcategoryid_array
。
假设我要在前端打印已启用类别的名称。 为此,我在第9行中使用了get_cat_name()
函数,该函数从变量$aa_getcategoryid_array[0]
检索第零个数组元素的ID的类别名称。 我在第10行再次重复了此过程,该行获取第一个数组元素的标题,即$aa_getcategoryid_array[1]
。
最后,要打印保存的标题,我创建了两个div,然后在其中分别回显每个名称。 (第21至27行)。
要为第零个和第一个数组元素打印标题,我必须启用两个此类。
在前端,类别标题的打印方式如下:
现在,我将在管理标签中创建此选项。
我在管理选项卡$aa_tab1
添加了multicheck-categories
类型选项。 它的ID是aa_multicheck_categories_opt_in_tab
。 但是这次我添加了一些新参数,即order和show_count参数。 我在上市的类别desc
顺序与show_count
设置为true,这将显示在每个类别的职位数。
现在,可以在“ 整洁的选项2 ”面板的选项 卡1内看到此选项。 类别的显示顺序也与以前不同,并且show_count
参数显示每个类别中的帖子数。
使用此代码获取保存的值。
使用此代码,我为三个连续的数组元素打印了标题。 该代码的主要部分非常相似,因此我在编写这些步骤的摘要:
标题打印如下:
接下来,我将在一个metabox中创建此选项。
现在,我在metabox $aa_metbox
创建了此选项。 它的唯一ID是aa_multicheck_categories_opt_in_metabox
。 其余参数相同。
在上面的屏幕截图中,有一个页面编辑屏幕,最后有一个名为Multicheck Categories Option的multicheck-categories
类型选项。
使用以下代码获取保存的值。
此代码仅打印第零个数组元素的类别名称。 但是您会发现第6行发生了变化。 getOption()
函数内部存在一个新参数。 这是get_the_ID()
函数,该函数用于检索特定的页面或帖子ID。
前端打印标题如下:
最后,让我们在主题定制器部分中创建此选项。
我在主题定制器部分$aa_section1
创建了一个multicheck-categories
类型选项。 其ID为aa_multicheck_categories_opt_in_sec
。
您可以在名为My Section的定制器部分中找到显示现有类别的复选框列表。
使用此代码。
这次,我将按照上面提到的相同步骤为所有五个现有类别打印标题。
这是在实时预览模式下观察到的更改的屏幕截图。
这种特殊类型可以用于各种创造性的用户案例。 例如,假设您要构建一个包含主页上不同部分的主题。 现在,您希望最终用户能够从特定部分的任何类别中选择帖子。
其他一些实际的例子是:
因此,这就是在Titan框架中使用multicheck
type选项的方式。 我仅提到了在您的项目中集成此选项的最简单方法。 但是,还有其他一些创新的更好的方法可以做到这一点。
例如,您可以编写一个带有控件的炫酷小部件,以启用/禁用边栏中最近帖子的显示。 这只是一个例子。 让我知道您是否提出了一个具有创造性的用户案例。
如有任何疑问,请发表评论或在Twitter上进行推广。