为此,我将自定义字段添加到类别:
add_action ( 'edit_category_form_fields', function( $tag ){
$cat_title = get_term_meta( $tag->term_id, '_pagetitle', true ); ?>
<tr class='form-field'>
<th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th>
<td>
<input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'>
<p class='description'><?php _e('Title for the Category '); ?></p>
</td>
</tr> <?php
});
add_action ( 'edited_category', function() {
if ( isset( $_POST['cat_title'] ) )
update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
});
然后我尝试使用ajax添加我自己的文本:
function data_person(){
$catname = $_POST['catName'];
$surnameCat = $_POST["surnameCat"];
$cityCat = $_POST["cityCat"];
$catDob = $_POST["dobCat"];
$catBio = $_POST["catBio"];
$cat_ID = get_cat_ID( sanitize_title_for_query($catname) );
// Check if category exists
if($cat_ID == 0) {
$cat_name = $catname;
$cat_sur = $surnameCat;
$cat_city = $cityCat;
$cat_dob = $catDob;
$cat_bio = $catBio;
$cat_slug = sanitize_title_with_dashes($cat_name);
$my_cat = array(
'cat_name' => $cat_name,
'category_description' => $cat_bio,
'cat_title' => $cat_sur,
'category_nicename' => $cat_slug,
'category_parent' => 0
);
if( wp_insert_category( $my_cat ) ) {
echo json_encode("Category added successfully");
} else {
echo json_encode("That category already exists");
}
} else {
echo json_encode("That category already exists");
}
exit;
}
但是我不确定如何将文本添加到新创建的自定义字段中,这不起作用:'cat_title' => $cat_sur,
,因为它没有保存单击后从输入字段发送的值。基本上应该代替'cat_title' =>
放什么?
更新
这是完整的代码,因此在function.php中,我们有:
add_action ( 'edit_category_form_fields', function( $tag ){
$cat_title = get_term_meta( $tag->term_id, '_pagetitle', true ); ?>
<tr class='form-field'>
<th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th>
<td>
<input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'>
<p class='description'><?php _e('Title for the Category '); ?></p>
</td>
</tr> <?php
});
add_action ( 'edited_category', function() {
if ( isset( $_POST['cat_title'] ) )
update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
});
add_action( 'wp_footer', 'ajax_Person' );
function ajax_Person() { ?>
<script type="text/javascript">
jQuery("#saveperson").on("click", function(e){
e.preventDefault();
person();
});
function person(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_person', catName: jQuery('#nameCat').val(), catSurnam: jQuery('#surnameCat').val(), catCity: jQuery('#cityCat').val(), catDob: jQuery('#dobCat').val(), catBio: jQuery('#bioCat').val() },
success: function(data) {
jQuery(".modal-body .help-text").html(data);
}
});
}
</script>
<?php }
add_action('wp_ajax_data_person' , 'data_person');
add_action('wp_ajax_nopriv_data_person','data_person');
function data_person(){
$catname = $_POST['catName'];
$surnameCat = $_POST["surnameCat"];
$cityCat = $_POST["cityCat"];
$catDob = $_POST["dobCat"];
$catBio = $_POST["catBio"];
$cat_ID = get_cat_ID( sanitize_title_for_query($catname) );
// Check if category exists
if($cat_ID == 0) {
$my_cat = array(
'cat_name' => $cat_name,
'category_description' => $cat_bio,
'category_nicename' => $cat_slug,
'category_parent' => 0
);
$cat_id = wp_insert_category( $my_cat );
if( ! is_wp_error( $cat_id ) && (int)$cat_id ) {
// NOW, add the metadata...
add_term_meta( $cat_id, '_pagetitle', $surnameCat );
echo json_encode("Category added successfully");
} else {
echo json_encode("That category already exists");
}
}
exit;
}
还有html:
<div class="col-xs-12">
<div class="add-pearson-form memory-form">
<div class="m-form-wrap">
<a href="javascript:;" class="frm_close"><i class="fa fa-times-circle"></i></a>
<div class="team_person">
<div class="form-group">
<div class="col-xs-12 col-sm-8 col-sm-offset-2">
<h4 class="text-center text-uppercase">aggiungi una persona</h4>
<input type="text" class="form-control" placeholder="NOME">
<input type="text" class="form-control" placeholder="COGNOME">
<input type="text" class="form-control" placeholder="CITTA">
<input type="text" class="form-control pickdate" placeholder="DATA DI NASCITA">
<textarea class="form-control" id="froala-editor" placeholder=" type ricordo"></textarea>
<div class="text-center">
<a href="" class="primary-btn text-uppercase">inserisci persona</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
您的问题的答案是使用add_term_meta函数。
请勿将a=2;
n=5; /* to generate numbers up to 27 */
for (i=3;i<n+3;i++)
{
a+=i;
printf("%d",a);
}
插入类别-wp_insert_category函数不接受/使用该参数。
相反,要从 cat_title
返回类别ID,然后使用该ID添加元值。
请参阅下面的修改/简化代码,并附上说明更改的注释:
wp_insert_category