此代码工作正常。我的问题是,当我提交时,它只检查父类别而不是子类别,即使我同时指定了父类别和子类别。
请我做错了什么。ohh如果查看html块7
是父类别,逗号后的数字是子类别。
<?php
if(isset($_POST[\'new_post\']) == \'1\') {
$post_title = $_POST[\'post_title\'];
$post_category = $_POST[\'cat\'];
$post_content = $_POST[\'post_content\'];
$new_post = array(
\'ID\' => \'\',
\'post_author\' => $user->ID,
\'post_content\' => $post_content,
\'post_title\' => $post_title,
\'post_status\' => \'publish\',
\'post_category\' => array($post_category)
);
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>
这是
html
<form method="post" action="" name="" onsubmit="return checkformf(this);">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea></br>
<ul><select name=\'cat\' id=\'cat\' class=\'postform\' >
<option class="level-0" value="7,95">child cat1</option>
<option class="level-0" value="7,100">child cat2</option>
<option class="level-0" value="7,101">child cat3</option>
<option class="level-0" value="7,94">child cat4</option>
</select>
</ul>
<input type="hidden" name="new_post" value="1"/>
<input class="subput" type="submit" name="submitpost" value="Post"/>
</form>
如果你需要更多信息,请告诉我。提前感谢
最合适的回答,由SO网友:MathSmath 整理而成
问题是,在PHP中无法生成这样的数组。尝试将包含逗号分隔列表的字符串转换为数组只会生成一个具有单个值的数组,即逗号分隔字符串。
您要使用php\'s explode function 创建阵列。它接受一个字符串,并基于任意分隔符将其拆分为一个真正的数组(在您的情况下,我们将使用逗号)。
尝试以下操作:
if(isset($_POST[\'new_post\']) == \'1\') {
$post_title = $_POST[\'post_title\'];
$arr_post_category = explode(\',\',$_POST[\'cat\']); // EXPLODE!
$post_content = $_POST[\'post_content\'];
$new_post = array(
\'ID\' => \'\',
\'post_author\' => $user->ID,
\'post_content\' => $post_content,
\'post_title\' => $post_title,
\'post_status\' => \'publish\',
\'post_category\' => $arr_post_category // NOW IT\'S ALREADY AN ARRAY
);
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}