创建具有wp_insert_post()

问题描述:

我有收集有关用户的产品数据,然后创建在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?

感谢

你确定你的页面模板源: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