首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress前端表单直接发布/起草帖子

Wordpress前端表单直接发布/起草帖子
EN

Stack Overflow用户
提问于 2012-07-03 14:21:52
回答 2查看 1.8K关注 0票数 0

我有一个Wordpress前端表单直接从我的主题发布/起草帖子:

代码语言:javascript
复制
<?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表单,它有以下功能:

代码语言:javascript
复制
<?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.";
}
?>

我想将它包含在表单中,并使用以下方法完成:

代码语言:javascript
复制
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';

但是如果‘中间’字段为空,它将忽略整个$description的值,如果‘中间’字段为空,我希望它只忽略第一个句子,并显示具有'end‘字段的第二个句子。

代码语言:javascript
复制
'a sentence ' . $_POST['end'] . ' with something in the END.';

如何让它像这样工作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-03 23:03:07

遵循下面这样的方法会让整个事情变得更好,更有意义。基本上,将描述变量设置为第一个句子,如果存在,则添加第二个比特。

代码语言:javascript
复制
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
}

此外,考虑转义字符串,这取决于它的用途。

票数 0
EN

Stack Overflow用户

发布于 2012-07-03 16:02:30

更改:

代码语言:javascript
复制
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';

至:

代码语言:javascript
复制
$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.': '' ;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11305486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档