如何在点击 ajax wordpress 时创建子类别?
How to create sub category on click ajax wordpress?
您好,我想在 ajax 上创建类别 wordpress。例如我有类别 A 和类别 B。
类别 A 有 2 个 child,类别 B 也有 2 个 child。 First user must choose Category A or Category B, when selected Category A the option will be change to child category A.
选择 Child 类别 A 后,用户将获得类别 A 中的大 child 选项。从最后一个选项将重定向到用户选择的 post 类别列表。在最后一页我们输出用户类别选择。 (喜欢 A 类,Child A 类,Grand Child A 类)
有什么建议吗?
解决方案:
对不起大家,我认为标题不正确。我在这里
找到了解决方案
所以我只是使用 jquery 在点击时隐藏和显示类别。希望对遇到同样问题的大家有所帮助
谢谢
你基本上需要两件事:wp_insert_term and some AJAX functionality。
From last option will be redirect to list of post category user choose.
此时,您可以触发您的 AJAX 请求。我建议您将分类术语 slugs 作为 data attribute 附加到 link:
<a class="category_selection" href="#" data-taxonomy="tax_a" data-parentterm="parent_a" data-term="term_a">Cat A</a>
您可以将 jQuery 函数绑定到点击事件:
$( document ).on( 'click', 'a.category_selection', function ( e ) {
var data = {
action: 'category_selection',
term: $( this ).data( 'term' ),
parent_term : $( this ).data( 'parentterm' ),
taxonomy = $( this ).data( 'taxonomy' ),
nonce: my_vars.ajax_nonce
};
$.ajax( {
type: 'POST',
url: my_vars.ajaxurl,
data: data,
async: true,
success: function ( response ) {
// Parse response
var returndata = JSON.parse( response );
$( '.container' ).html( returndata );
}
} );
});
现在您需要一个 PHP 可以附加到操作 category_selection
的回调函数。在此函数中,您会将术语添加到您的类别中。
add_filter( 'wp_ajax_category_selection', 'add_term' );
add_filter( 'wp_ajax_nopriv_category_selection', 'add_term' );
function add_term() {
check_ajax_referer( 'my_ajax', 'nonce' );
$taxonomy = filter_input( INPUT_POST, 'taxonomy', FILTER_SANITIZE_STRING );
// Sanitize the other input variables
$newterm = wp_insert_term( $term, $taxonomy, array( 'parent' => $parent_term ) );
if ( ! is_wp_error( $newterm ) ) :
echo json_encode( array( 'Success' ) );
endif;
echo json_encode( array( 'Failure' ) );
exit;
}
这应该可以让您大致了解如何处理它。这不适用于复制和粘贴,因为仍然缺少一些东西,比如创建一个 AJAX 随机数或你需要的 my_vars
javascript 对象。您可以在我在上面 link 编辑的 Codex 文章中找到必要的信息。
您好,我想在 ajax 上创建类别 wordpress。例如我有类别 A 和类别 B。
类别 A 有 2 个 child,类别 B 也有 2 个 child。 First user must choose Category A or Category B, when selected Category A the option will be change to child category A.
选择 Child 类别 A 后,用户将获得类别 A 中的大 child 选项。从最后一个选项将重定向到用户选择的 post 类别列表。在最后一页我们输出用户类别选择。 (喜欢 A 类,Child A 类,Grand Child A 类)
有什么建议吗?
解决方案:
对不起大家,我认为标题不正确。我在这里
所以我只是使用 jquery 在点击时隐藏和显示类别。希望对遇到同样问题的大家有所帮助
谢谢
你基本上需要两件事:wp_insert_term and some AJAX functionality。
From last option will be redirect to list of post category user choose.
此时,您可以触发您的 AJAX 请求。我建议您将分类术语 slugs 作为 data attribute 附加到 link:
<a class="category_selection" href="#" data-taxonomy="tax_a" data-parentterm="parent_a" data-term="term_a">Cat A</a>
您可以将 jQuery 函数绑定到点击事件:
$( document ).on( 'click', 'a.category_selection', function ( e ) {
var data = {
action: 'category_selection',
term: $( this ).data( 'term' ),
parent_term : $( this ).data( 'parentterm' ),
taxonomy = $( this ).data( 'taxonomy' ),
nonce: my_vars.ajax_nonce
};
$.ajax( {
type: 'POST',
url: my_vars.ajaxurl,
data: data,
async: true,
success: function ( response ) {
// Parse response
var returndata = JSON.parse( response );
$( '.container' ).html( returndata );
}
} );
});
现在您需要一个 PHP 可以附加到操作 category_selection
的回调函数。在此函数中,您会将术语添加到您的类别中。
add_filter( 'wp_ajax_category_selection', 'add_term' );
add_filter( 'wp_ajax_nopriv_category_selection', 'add_term' );
function add_term() {
check_ajax_referer( 'my_ajax', 'nonce' );
$taxonomy = filter_input( INPUT_POST, 'taxonomy', FILTER_SANITIZE_STRING );
// Sanitize the other input variables
$newterm = wp_insert_term( $term, $taxonomy, array( 'parent' => $parent_term ) );
if ( ! is_wp_error( $newterm ) ) :
echo json_encode( array( 'Success' ) );
endif;
echo json_encode( array( 'Failure' ) );
exit;
}
这应该可以让您大致了解如何处理它。这不适用于复制和粘贴,因为仍然缺少一些东西,比如创建一个 AJAX 随机数或你需要的 my_vars
javascript 对象。您可以在我在上面 link 编辑的 Codex 文章中找到必要的信息。