wp_update_post 来自锚点击的类别
wp_update_post category from anchor click
想使用 wp_update_post
更改和更新 post category
。应通过单击 <a>
或 <button>
更改类别
据我所知,这应该将更新应用于 post;
$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>';
编辑 - 更多信息
更新Post 主题功能中的代码 - 尚未测试。
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(); ?>
您需要向您连接的脚本发出 AJAX 请求。在 WordPress 中连接 AJAX 有点奇怪,但与其尝试为您做这件事,您应该查看 WordPress Codex 中的文档:
https://codex.wordpress.org/AJAX_in_Plugins
您将添加一个基本上将从表单输入中传递 post 类别 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"
,然后每个 option
的 value
作为 WP 模板循环中的 $cat_id
您需要使用 AJAX 创建一个包含 cat_id
参数的 POST
请求(从 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
)。 cat_id
从 select 输入中获取,然后在 AJAX 请求的 data
对象中发送。
使用那个 data
对象,我可以在该脚本中的 PHP 中获取 $cat_id
。
那个action
名字真的是关键。
想使用 wp_update_post
更改和更新 post category
。应通过单击 <a>
或 <button>
据我所知,这应该将更新应用于 post;
$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>';
编辑 - 更多信息
更新Post 主题功能中的代码 - 尚未测试。
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(); ?>
您需要向您连接的脚本发出 AJAX 请求。在 WordPress 中连接 AJAX 有点奇怪,但与其尝试为您做这件事,您应该查看 WordPress Codex 中的文档:
https://codex.wordpress.org/AJAX_in_Plugins
您将添加一个基本上将从表单输入中传递 post 类别 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"
,然后每个option
的value
作为 WP 模板循环中的$cat_id
您需要使用 AJAX 创建一个包含
cat_id
参数的POST
请求(从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
)。cat_id
从 select 输入中获取,然后在 AJAX 请求的data
对象中发送。使用那个
data
对象,我可以在该脚本中的 PHP 中获取$cat_id
。
那个action
名字真的是关键。