我有一个WordPress前端表格直接从我的主题发布/草稿: -
<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
$title = $_POST["title"];
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';
}
$tags = $_POST["tags"];
$post_cat = $_POST['cat'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $post_cat, // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => 'draft', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post', //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( '/post-submitted-draft' );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
?>
我有一个简单的PHP表单,它具有以下功能: -
<?php
if(!empty($_POST['middle'])) {
echo "a sentence".$_POST['middle']." with something in the MIDDLE.";
}
if(!empty($_POST['end'])) {
echo "a sentence".$_POST['end']." with something in the END.";
}
?>
我希望将其包含在表单中,然后使用以下方法完成: -
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';
但是如果中间字段的字段会忽略$ description的全部值。是空的,我希望它忽略第一句话,如果中间的字段是&#39;是空的并显示第二个句子,其中包含&#39; end&#39;即。
'a sentence ' . $_POST['end'] . ' with something in the END.';
如何让它像这样工作?
答案 0 :(得分:0)
更改:
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';
到:
$description = (!empty($_POST['middle']))? 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE.': '' ;
$description .= (!empty($_POST['end']))? 'a sentence ' . $_POST['end'] . ' with something in the END.': '' ;
答案 1 :(得分:0)
遵循下面的方法将使整个事情变得更好,更有意义。基本上,将描述变量设置为第一个句子,如果存在则添加第二个位。
if(!empty($_POST['middle'])) {
$description = "a sentence".$_POST['middle']." with something in the MIDDLE.";
}
if(!empty($_POST['end'])) {
$description .= "a sentence".$_POST['end']." with something in the END.";
}
if(isset($description)) {
// do something with description
}
另外,请考虑根据您使用的字符串来转义字符串。