0

I have created a page per Code with:

$my_post = array(
                          'post_title'    => $mytitle,                            
                          'post_status'   => 'publish',
                          'post_type'     => 'page',
                          'post_name'     => $myurl,                              
                          'page_template' => 'Agenden.php'
                        );


                        $ID = wp_insert_post( $my_post );

i also have a function in functions.php to do a $post_type_object->template = ... on a 'page'; but this function does not run for pages done with code top;

My question: How to do a page per Code with pagetemplate (Agenden.php) and give this blocktemplates

0

There are two completely different templates in WordPress - page templates and block templates. Your wp_insert_post assigns page template, which must exist in your theme directory and should be properly created (see docs). WP will automatically detect and use it to render this page. This is in no way related to $post_type_object->template.

4
  • I know; What i want to do: I have a page template "Agenden.php"; Then i want to create a new page with this template in php. This is already done (Code top). My problem is that i want to feed blocktemplates only if pagetemplate "Agenden.php" is used. For pages without page template i have a function in the function.php with $post_type_object->template and it runs, but only for pages without template; How to get a seperate blocktemplate for the pagetemplate "Agenden"?
    – Checkpoint
    Jan 31 '20 at 15:04
  • Ok, now I see. Your problem is not pagetempate, but the way how gutenberg uses block templates. AFAIK they are applied only when gutenberg creates initial post content. In your case content is already there (empty). To fix this you may try to create initial post_content (as created per block template when you create page manually) in your wp_insert_post array. Jan 31 '20 at 15:42
  • is there a example how to initial at wp_insert_post? I cant find anything like that...
    – Checkpoint
    Mar 2 '20 at 17:30
  • No code example, as in general it is the same wp_insert_post. You just need to set post_content value. The value is something that matches your blocks template for this type of posts. The best way to get it in my opinion is to create new "empty" post manually (ie, using WP admin interface), save it, then use DB client to copy saved post_content text and use it as initial value for your wp_insert_post. Here's my other answer about the same problem - wordpress.stackexchange.com/questions/357566/… Mar 3 '20 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.