php - 如何从模板页面在 WordPress 中创建新页面?

标签 php wordpress

我在一个 Wordpress 项目中,正在开发一个树结构来实现项目和子项目及其子项目并继续。

当用户创建新级别的项目时,我可以设法在数据库中创建一个表。但每个项目都需要一个特定的WordPress页面。当用户选择新项目时,我很难创建一个页面。当未登录 WordPress 用户尝试创建页面时,如何创建 WordPress 页面?

简而言之,我想通过从 WordPress 外部单击按钮来在 WordPress 内创建一个页面。请注意,用户没有登录 WordPress,他与 WordPress 没有任何关系。这是在前端发生的过程。它是如何实现的?

最佳答案

我有完全相同的设置,并且我使用名为项目的自定义帖子类型来实现此目的。设置方法如下:

  1. 在functions.php中注册您的自定义帖子类型

    add_action( 'init', 'rajmohan_register_project_post_type' );
    
    function rajmohan_register_project_post_type() {
    
        // Set UI labels for Custom Post Type
        $labels = array(
            'name'                  => 'Projects',
            'singular_name'         => 'Project',
            'menu_name'             => 'Post Types',
            'name_admin_bar'        => 'Post Type',
            'archives'              => 'Project Archives',
            'parent_item_colon'     => 'Parent Project:',
            'all_items'             => 'All Projects',
            'add_new_item'          => 'Add New Project',
            'add_new'               => 'Add New',
            'new_item'              => 'New Project',
            'edit_item'             => 'Edit Project',
            'update_item'           => 'Update Project',
            'view_item'             => 'View Project',
            'search_items'          => 'Search Project',
            'not_found'             => 'Not found',
            'not_found_in_trash'    => 'Not found in Trash',
            'featured_image'        => 'Featured Image',
            'set_featured_image'    => 'Set featured image',
            'remove_featured_image' => 'Remove featured image',
            'use_featured_image'    => 'Use as featured image',
            'insert_into_item'      => 'Insert into Project',
            'uploaded_to_this_item' => 'Uploaded to this Project',
            'items_list'            => 'Projects list',
            'items_list_navigation' => 'Project list navigation',
            'filter_items_list'     => 'Filter Projects list',
        );
    
        // Set other options for Custom Post Type
        $args = array(
            'label'               => 'Project',
            'description'         => 'Projects',
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => false,
            'exclude_from_search' => true,
            'publicly_queryable'  => true,
            'capability_type'     => 'post',
        );
    
        // Registering your Custom Post Type
        register_post_type( 'project', $args );
    }
    
  2. 为创建新项目的页面创建模板,例如 new-project.php

    <?php
    /*
        Template Name: rajmohan-new-project
    */
    
    // Create post object
    $project = array(
        'post_type'   => 'project',
        'post_status' => 'publish',
        'post_author' => 1,
    );
    
    // Insert the post into the database
    $id = wp_insert_post( $project );
    $permalink = get_permalink( $id );
    wp_redirect( $permalink );
    
  3. 在主题文件夹的根目录中创建一个名为 single-project.php 的模板。该模板将显示单个项目的数据。

    <?php
    echo 'Hello world!';
    // This is where you would print out any info about the project which are stored in the global post object
    
  4. 转到 WordPress 仪表板并创建一个名为“新项目”的新页面,然后为其分配 new-project.php 模板。

  5. 最后,您需要更新重写规则以包含新的自定义帖子类型,方法是转到 WordPress 仪表板中的“设置 > 永久链接”并单击保存更改。

  6. 现在,当您的访问者导航到“新项目”页面 (example.com/new-project) 时,将创建一个新项目,并且他们将被重定向以查看该项目(WordPress 将使用 single-project.php我们创建该项目是为了显示数据)。

关于php - 如何从模板页面在 WordPress 中创建新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39093572/

相关文章:

PHP Mysql 更新问题

php - Laravel 上传文件到项目目录外的不同存储

html - CSS - 悬停触发另一个悬停

php - WordPress <?php body_class(); ?> 问题

php - AWS S3 PHP 我可以将文件直接上传到存储桶中的文件夹吗

php - 对具有较高值的​​列表进行排序

PHP语言问题

php - 检测何时在 Woocommerce 中单击 "remove"优惠券按钮

php - 删除 wp_get_attachment_image 图像大小

wordpress - 自定义 WP 帖子循环仅显示一个帖子