我有收集有關用戶的產品數據,然後創建在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:
現在我想創建一個從同一第二頁表單數據,這個數據以不同的方式顯示數據的不同部分。所以我添加了第二塊代碼,從下面的$ 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:
我知道我已經設置高達certificate.php正確作爲模板(套/ *模板名稱:證書* /頂部),因爲頁面編輯模板下拉菜單,包括證書:
因此,有沒有人看到爲什麼我不能創建第二頁的模板設置爲certificate.php?
感謝
是的,你釘了它!雖然listing.php直接在主題下,但我將certificate.php放在主題下的證書文件夾中,所以它需要是certificate/certificate.php。謝謝! – Steve
很高興我能幫上忙! – MrZiggyStardust