【问题标题】:Wordpress - How to automatically create a child pageWordpress - 如何自动创建子页面
【发布时间】:2015-01-25 10:29:19
【问题描述】:

我的问题是,用户创建了包含内容的新页面,并且效果很好且符合预期。他们要做的是添加另一个没有内容的页面,选择正确的模板并将其作为他们刚刚创建的页面的父级。第二个空白页面在那里,因为它让用户查看网页以不同于主页列出它的方式查看内容。 wordpress 有没有办法自动将此空白页添加到这些模板中?

这是页面结构的工作原理

主页 --> Wordpress 页面(用户创建) --> Wordpress 页面(列出与父页面相同的内容,但格式不同。这是 wordpress 用户创建的空白页面。)

那么有没有办法自动创建那个额外的子页面,或者有没有办法我可以添加该代码并链接到原始的 wordpress 页面模板?

谢谢, 瑞恩

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您可以在发布页面后添加操作。在此操作中,您可以添加具有属性的新页面。

    你可以试试这个:

    function on_page_publish( $ID, $post ) {
    
        $my_post = array(
          'post_title'    => 'My post',
          'post_content'  => 'This is my post.',
          'post_status'   => 'publish',
          'post_author'   => 1,
        );
    
        // Insert the post into the database
        wp_insert_post( $my_post );
    
    }
    add_action(  'publish_page',  'on_page_publish', 10, 2 );
    

    工作将对这里的所有人有用:

    $post = array(
      'ID'             => [ <post id> ] // Are you updating an existing post?
      'post_content'   => [ <string> ] // The full text of the post.
      'post_name'      => [ <string> ] // The name (slug) for your post
      'post_title'     => [ <string> ] // The title of your post.
      'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // Default 'draft'.
      'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // Default 'post'.
      'post_author'    => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
      'ping_status'    => [ 'closed' | 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
      'post_parent'    => [ <post ID> ] // Sets the parent of the new post, if any. Default 0.
      'menu_order'     => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
      'to_ping'        => // Space or carriage return-separated list of URLs to ping. Default empty string.
      'pinged'         => // Space or carriage return-separated list of URLs that have been pinged. Default empty string.
      'post_password'  => [ <string> ] // Password for post, if any. Default empty string.
      'guid'           => // Skip this and let Wordpress handle it, usually.
      'post_content_filtered' => // Skip this and let Wordpress handle it, usually.
      'post_excerpt'   => [ <string> ] // For all your post excerpt needs.
      'post_date'      => [ Y-m-d H:i:s ] // The time post was made.
      'post_date_gmt'  => [ Y-m-d H:i:s ] // The time post was made, in GMT.
      'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
      'post_category'  => [ array(<category id>, ...) ] // Default empty.
      'tags_input'     => [ '<tag>, <tag>, ...' | array ] // Default empty.
      'tax_input'      => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty.
      'page_template'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
    );  
    

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多