如何从模板页面在wordpress中创建新页面?

问题描述:

我在一个Wordpress项目,我正在开发一个树结构来实现项目和子项目及其子项目,并继续。如何从模板页面在wordpress中创建新页面?

我可以管理数据库中创建表时用户创建的项目新的水平。但是每个项目都需要一个特定的wordpress页面。我努力在用户进入新项目时获得创建的页面。当一个unlogined to 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. 发布类型创建,创建新的项目页面的模板,例如新的项目。 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的仪表板,并创建一个名为“新建项目”新页和新project.php模板分配给它。

  5. 最后,您需要更新您的重写规则,以在您的WordPress仪表板中点击“设置>固定链接”并包含新的自定义帖子类型,然后点击保存更改。

  6. 现在,当您的访问者导航到新建项目页面(example.com/new-project)时,将创建一个新项目并将它们重定向到查看项目(WordPress将使用single-project.php我们创建用于显示此项目的数据)。

从管理面板将一个模板页testtemplate.php在活动主题目录中注释行

/** 
* Template Name: Test Template 
*/ 

然后,当你创建新的网页时,在选择模板下拉列表中。

从哪里可以选择相应的模板。

创建新的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(); ?> 

之后,进入管理面板创建新页面。将会有一个下拉菜单,这个模板名将被列出。你可以从那里选择这个模板。

+0

谢谢Dinesh它正在工作..超级!! – RAJMOHAN

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

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