在WordPress管理中,保存使用jQuery排序的post类型排序顺序是通过使用自定义字段(custom fields)和jQuery来实现的。
首先,需要在post类型的编辑页面中添加一个自定义字段,用于保存排序顺序的值。可以使用WordPress的add_meta_box函数来添加自定义字段。
然后,在前端页面中使用jQuery来实现排序功能。可以使用jQuery UI的sortable方法来实现拖拽排序。当排序完成后,通过jQuery将排序结果保存到自定义字段中。
以下是一个完整的实现步骤:
function add_custom_meta_box() {
add_meta_box(
'post_order',
'Post Order',
'render_custom_meta_box',
'post',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function render_custom_meta_box($post) {
$order = get_post_meta($post->ID, 'post_order', true);
?>
<label for="post_order">Order:</label>
<input type="text" id="post_order" name="post_order" value="<?php echo esc_attr($order); ?>">
<?php
}
function save_custom_meta_box($post_id) {
if (array_key_exists('post_order', $_POST)) {
update_post_meta(
$post_id,
'post_order',
sanitize_text_field($_POST['post_order'])
);
}
}
add_action('save_post', 'save_custom_meta_box');
function enqueue_custom_scripts() {
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
jQuery(document).ready(function($) {
$('#sortable').sortable({
update: function(event, ui) {
var order = '';
$('#sortable li').each(function() {
order += $(this).data('post-id') + ',';
});
$('#post_order').val(order);
}
});
});
<ul id="sortable">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'meta_value',
'meta_key' => 'post_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
?>
<li data-post-id="<?php the_ID(); ?>"><?php the_title(); ?></li>
<?php
}
wp_reset_postdata();
?>
</ul>
以上代码将在页面中显示一个可排序的列表,每个列表项都有一个data-post-id属性,用于保存对应的文章ID。当排序完成后,将排序结果保存到自定义字段post_order中。
这样,通过以上步骤,就可以在WordPress管理中保存使用jQuery排序的post类型排序顺序了。
推荐的腾讯云相关产品:腾讯云服务器(CVM)和腾讯云数据库(TencentDB)。
腾讯云服务器(CVM)是一种弹性计算服务,提供可扩展的云服务器实例,适用于各种应用场景。您可以根据实际需求选择不同的配置,包括计算能力、存储容量、网络带宽等。了解更多信息,请访问腾讯云服务器产品介绍页面:腾讯云服务器
腾讯云数据库(TencentDB)是一种高性能、可扩展的云数据库服务,支持多种数据库引擎,包括MySQL、SQL Server、MongoDB等。您可以根据实际需求选择不同的数据库类型和规格,以满足应用程序的需求。了解更多信息,请访问腾讯云数据库产品介绍页面:腾讯云数据库
领取专属 10元无门槛券
手把手带您无忧上云