想要使用category
更改和更新帖子wp_update_post
。应点击<a>
或<button>
据我所见,这应该将更新应用于帖子;
$live_paused = array(
'post_category' => 6
);
// Update the post into the database
wp_update_post( $live_paused );
但是如何将此功能添加到此
echo '<a href="" id=""><i class="fa fa-pause"></i></a>';
修改 - 更多信息
更新主题功能中的邮政编码 - 尚未测试。
function live_paused_status( $post_id ){
if (current_user_can('edit_post', $post->ID)) {
$live_paused = array(
'post_category' => 6
);
echo '<a href="" id=""><button title="" data-toggle="tooltip" class="campaigns-link-button" type="button" data-original-title="Pause Campaign"><i class="fa fa-pause"></i></button></a>';
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'live_paused_status');
// update the post, which calls save_post again
wp_update_post( $live_paused );
// re-hook this function
add_action('save_post', 'live_paused_status');
}
}
add_action('save_post', 'live_paused_status');
循环
<?php $query = new WP_Query( array( 'post_type' => 'campaigns'));?>
<?php if ($query->have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="card">
<div class="card-footer">
<div class="row">
<div class="col-4 campaigns-link">
<?php echo live_paused_status(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
答案 0 :(得分:1)
您需要向连接的脚本发出AJAX请求。在WordPress中连接AJAX有点奇怪,但是你应该查看WordPress Codex中的文档,而不是尝试为你做这些:
https://codex.wordpress.org/AJAX_in_Plugins
您将添加一个基本上从表单输入传递帖子类别ID的操作,您将使用JS在POST请求中发送该操作,然后您将获取该ID并更新该类别。
希望这有帮助。
修改强>
<?php
add_action( 'wp_ajax_custom_update_category', 'custom_update_category' );
function custom_update_category() {
$cat_id = sanitize_text_field( $_POST['cat_id']); //passed from AJAX. Make sure to escape it just in case.
# update the category here with $cat_id
}
尝试更多地改进我的答案,而不是为你编写:
select
输入name="cat_id"
,然后每个value
的{{1}}为option
您需要使用包含$cat_id
参数的AJAX创建POST
请求(从cat_id
输入的select
中提取此参数)。这是我过去做的一个。
value
请注意,我将 var cat_id = $('#product-cat-select').val();
$.ajax({
type : "POST",
url : ajaxurl,
data : {
action: "custom_update_category",
cat_id: cat_id
},
success: function(response) {
console.log( response );
}
});
命名为与我的示例action
中add_action
的后缀相同的内容。custom_update_category
。正在从select输入中获取cat_id
,然后在AJAX请求的data
对象中发送。{/ p>
使用该data
对象,我可以在该脚本中获取PHP中的$cat_id
。
action
名称确实是关键。