我们确实有kirki(一个主题定制工具包(sortable field
。但我想在节本身应用相同的逻辑,而不是创建另一个具有可排序字段的节。我使用选项而不是主题mods,只是为了更好地可视化。到目前为止我尝试过的事情:在functions.php
中,我有下面的代码来在选项表中创建一个选项行:
$sortable_sections = get_option('sortable_sections');
if(!isset($sortable_sections) OR empty($sortable_sections)){
add_option('sortable_sections', array('eat', 'pray','love'));
}
在名为kirki-config.php
的文件中创建节的代码,functions.php
中需要该代码,如下所示
$sortable_sections = get_option('sortable_sections');
foreach($sortable_sections as $sortable_section){
Kirki::add_section( $sortable_section, array(
'title' => esc_attr__( strtoupper($sortable_section), 'airspace' ),
'description' => esc_attr__( 'Insert content', 'airspace' ),
'panel' => 'frontpage_panel',
) );
}
上面的代码分别创建了id为eat、pray和love的三个部分。
js
中的代码如下:(挂接到admin_enqueue_scripts
的文件(
jQuery( document ).ready(function($) {
$('#sub-accordion-panel-frontpage_panel').sortable({
items: '.control-section-kirki-default',
axis : 'y',
cursor: 'move,
update: function(){
//here i want the code which gets the updated reordered array and passed to a
//php file using .sortable('serialize'), only after clicking the publish
// button. the problem is the publish button is disabled even after
// sections got new positions.
}
});
});
在php文件中,我想要如下代码:
$new_array = $_POST['accordion-section'];
update_option('sortable_sections', $new_array);
所以有两个步骤我有问题要完成。1.启用和禁用发布按钮2.点击按钮后,js变量转到php文件并更新选项。
如何实现它,有没有更好的方法来实现它?
我会在单击"发布"时获取您的数据。
(function($) {
$("#sub-accordion-panel-frontpage_panel").sortable({
items: "> .control-section-kirki-default",
axis : "y",
cursor: "move"
});
$("#publish").click(function(e){
e.preventDefault();
var panelOrder = $("#sub-accordion-panel-frontpage_panel").sortable("serialize");
// Submit data to PHP
});
})(jQuery);
由于你还没有提供MCVE,这是我能建议的全部内容。
更新
$("#sub-accordion-panel-frontpage_panel").sortable({
items: "> .control-section-kirki-default",
axis : "y",
cursor: "move",
update: function(){
$(this).trigger("change");
}
});