2012-07-03 32 views
0

我有一個WordPress的前端形式直接從我的主題發佈帖子/草案: -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.'; 

,但是如果'middle'的字段爲空,並且我希望它在'middle'字段爲空的情況下只忽略第一個句子,並顯示第二個句子'field'爲'結束'即

'a sentence ' . $_POST['end'] . ' with something in the END.'; 

如何使它像這樣工作?

回答

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 
} 

另外,根據您使用的字符串,考慮轉義字符串。

+0

如果我有一個「開始」字段作爲開始句子,並且它是必填字段,那麼用戶將不得不填寫它?在上面的代碼之前,我應該用以下簡單的方式添加它嗎? '$ description =「a sentence」。$ _ POST ['start']。「在START中有一些東西。」;' – atif

+0

感謝您的回答,您可以查看一下這個與php和MySQL相關的問題。 http://stackoverflow.com/questions/11282461/how-to-get-multiple-row-values – atif

+0

你能告訴這個嗎? http://stackoverflow.com/questions/11333589/if-post-is-empty-multiple-function – atif

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.': '' ; 
相關問題