【问题标题】:How to create a new page in wordpress from template page?如何从模板页面在 wordpress 中创建新页面?
【发布时间】:2016-08-23 06:10:14
【问题描述】:

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

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

简而言之,我想通过单击 wordpress 外部的按钮在 wordpress 内部创建一个页面。保持用户没有在 wordpress 上登录,他与 wordpress 无关。这是发生在前端的过程。它是如何实现的?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我有完全相同的设置,我正在使用一个名为 project 的自定义帖子类型来实现这一点。设置方法如下:

    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我们创建来显示这个项目的数据)。

    【讨论】:

      【解决方案2】:

      在主题目录/文件夹中创建新的 PHP 页面(即 test.php)。

      <?php
      /**
      * Template Name: Test Template
      *
      * This is the template that displays all pages by default.
      * Please note that this is the WordPress construct of pages and that
      * other "pages" on your WordPress site will use a different template.
      *
      * @package WordPress
      * @subpackage Twenty_Sixteen
      * @since Twenty Sixteen 1.0
      */
      
      get_header(); ?>
      
      <div id="primary" class="content-area">
      <main id="main" class="site-main" role="main">
          <?php
          // Start the loop.
          while ( have_posts() ) : the_post();
      
              // Include the page content template.
              get_template_part( 'template-parts/content', 'page' );
      
              // If comments are open or we have at least one comment, load up the comment template.
              if ( comments_open() || get_comments_number() ) {
                  comments_template();
              }
      
              // End of the loop.
          endwhile;
          ?>
      
      </main><!-- .site-main -->
      
      <?php get_sidebar( 'content-bottom' ); ?>
      
      </div><!-- .content-area -->
      
       <?php get_sidebar(); ?>
      <?php get_footer(); ?>
      

      之后转到管理面板创建新页面。将列出一个带有此模板名称的下拉列表。您可以从那里选择此模板。

      【讨论】:

        【解决方案3】:

        将模板页面 testtemplate.php 放在带有注释行的活动主题目录中

        /**
         * Template Name: Test Template
         */
        

        然后在您创建新页面时从管理面板中选择模板下拉列表中可用。

        您可以从中选择相应的模板。

        【讨论】:

          【解决方案4】:

          我认为您需要查看wp_insert_post()。此功能使您能够动态创建帖子。 (这里的“帖子”是指任何类型的帖子/页面/自定义帖子类型。)

          我会创建一个自定义帖子类型(实际上是“自定义页面类型”),并让用户使用 wp_insert_post() 创建一个新类型。 wp_insert_post() 需要标题和内容(至少),并通过清理传递数据(因此它可以尽可能安全地由用户创建帖子)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多