2016-11-24 88 views
0

我有收集有關用戶的產品數據,然後創建在WordPress一個頁面創建具有wp_insert_post()

$my_post = array(
      'post_content' => "My page content", 
      'post_title'  => $product_title, 
      'post_name'  => $product_title, 
      'post_type'  => 'page', // must be 'page' to accept the 'page_template' below 
      'page_template' => "listing.php", 
      'post_status' => "publish" 
     ); 
     $ID = wp_insert_post($my_post); 
     $permalink = get_permalink($ID); 
     echo "<br />ID for new page is $ID, Permalink for new page is $permalink"; 

表單數據放入元變量的頁面ID和一個表單頁面listing.php模板文件將其從中拉出並構建HTML以顯示產品頁面。這一切工作正常,我可以看到,頁面元變量,_wp_page_template,得到正確設置爲我指定的模板文件,listing.php:

enter image description here

現在我想創建一個從同一第二頁表單數據,這個數據以不同的方式顯示數據的不同部分。所以我添加了第二塊代碼,從下面的$ my_cert開始,創建了第二個頁面,並指定了一個不同的模板certificate.php,它知道如何構建第二版數據。

$my_post = array(
      'post_content' => "My page content", 
      'post_title'  => $product_title, 
      'post_name'  => $product_title, 
      'post_type'  => 'page', // must be 'page' to accept the 'page_template' below 
      'page_template' => "listing.php", 
      'post_status' => "publish" 
     ); 
     $ID = wp_insert_post($my_post); 
     $permalink = get_permalink($ID); 
     echo "<br />ID for new page is $ID, Permalink for new page is $permalink"; 

    $my_cert = array(
     'post_content' => "My certificate", // post_content is required 
     'post_title'  => "My certificate", // post_title is required 
     'post_name'  => "My certificate", 
     'post_type'  => 'page', // must be 'page' to accept the 'page_template' below 
     'page_template' => "certificate.php", 
     'post_status' => "publish" 
    ); 
    $CERT_ID = wp_insert_post($my_cert); 
    $cert_permalink = get_permalink($CERT_ID); 
    echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink"; 

但是當我看在創建的第二個頁面的元數據,模板設置爲「默認」,而不是certificate.php:

enter image description here

我知道我已經設置高達certificate.php正確作爲模板(套/ *模板名稱:證書* /頂部),因爲頁面編輯模板下拉菜單,包括證書:

enter image description here

因此,有沒有人看到爲什麼我不能創建第二頁的模板設置爲certificate.php?

感謝

回答

1

你確定你的頁面模板源:certificate.php是:certificate.php?而不是:templates/certificate.php或類似的東西。看看你的主題文件夾,並100%的頁面模板路徑。檢查您的拼寫或頁面模板路徑或名稱中的拼寫錯誤。它必須完全匹配。

如果你還有問題我會考慮和調試的源代碼:它可能是這部分wp_insert_post()

if (! empty($postarr['page_template']) && 'page' == $data['post_type']) { 
    $post->page_template = $postarr['page_template']; 
    $page_templates = wp_get_theme()->get_page_templates($post); 
    if ('default' != $postarr['page_template'] && ! isset($page_templates[ $postarr['page_template'] ])) { 
     if ($wp_error) { 
      return new WP_Error('invalid_page_template', __('The page template is invalid.')); 
     } 
     update_post_meta($post_ID, '_wp_page_template', 'default'); 
    } else { 
     update_post_meta($post_ID, '_wp_page_template', $postarr['page_template']); 
    } 
} 

所以失敗:

if ('default' != $postarr['page_template'] && ! isset($page_templates[ $postarr['page_template'] ])) 

嘗試修改:wp-includes/post.php去定義如下:function wp_insert_post()行:2872.然後在行3312上添加一個新行用於調試目的。

echo '<pre>'; 
    print_r($page_templates); 
    echo '</pre>'; 
    die(); 

確保您的certificate.php位於該陣列中。請記住在繼續之前刪除調試代碼。這應該給你一些答案。

+0

是的,你釘了它!雖然listing.php直接在主題下,但我將certificate.php放在主題下的證書文件夾中,所以它需要是certificate/certificate.php。謝謝! – Steve

+0

很高興我能幫上忙! – MrZiggyStardust