使用子主题方式对Avada主题进行二次开发,开发过程中需要添加部分后台设置项,本来打算直接写自己的设置页面,后来想想,还是添加到Avada默认的设置项里面比较和谐。Avada主题用的设置框架是ReDux的自改版本,在官方支持页面找了半天也没找到相关文档,只能自己看代码找了,一般这种大型主题,都会预留钩子方便扩展,找到正确的钩子,再根据相应的数据结构进行扩展即可。示例代码如下:
/**
* 添加后台设置项
*/
add_filter( 'avada_options_sections', function($sections) {
//获取全部顶级分类
$categories = get_categories( array(
'parent' => 0,
'hide_empty' => false,
) );
if ( ! $categories ) {
return $sections;
}
$new_categories = array();
//循环全部顶级分类,组合成适合选项使用的新数组
foreach ($categories as $category) {
$new_categories[$category->term_id] = $category->name;
}
$sections['avada_products_settings'] = [
'label' => esc_html__( '定制设置', 'Avada' ),
'description' => '',
'id' => 'avada_products_settings',
'icon' => 'el el-shopping-cart',
'type' => 'sub-section',
'fields' => [
'products_category' => [
'label' => esc_html__( '产品分类', 'Avada' ),
'description' => esc_html__( '选择作为产品的分类目录,只列出一级分类,选择以后,其下的子分类自动继承', 'Avada' ),
'id' => 'products_category',
'choices' => $new_categories,
'type' => 'select',
],
'products_form_title' => [
'label' => esc_html__( '产品页表单标题', 'Avada' ),
'description' => esc_html__( '留空则不显示', 'Avada' ),
'id' => 'products_form_title',
'type' => 'text',
],
'products_form' => [
'label' => esc_html__( '产品页表单短代码', 'Avada' ),
'description' => esc_html__( '使用Contact Form 7插件生成的短代码', 'Avada' ),
'id' => 'products_form',
'type' => 'textarea',
],
'products_form_note_info' => [
'label' => '',
'description' => '<div>' . __( '<strong>提示:</strong> 要使表单中包含产品名称和产品链接,必须要在表单中添加下面两个隐藏表单项。<br><br><code><input type="hidden" name="product_name" id="product_name" value=""></code><br><code><input type="hidden" name="product_link" id="product_link" value=""></code>', 'Avada' ) . '</div>',
'id' => 'products_form_note_info',
'type' => 'custom',
],
]
];
return $sections;
}, 99, 1 );
效果如下: