簡體   English   中英

如何在Wordpress中從模板頁面創建新頁面?

[英]How to create a new page in wordpress from template page?

我在一個Wordpress項目中,並且正在開發樹結構以實施項目和子項目及其子項目,然后繼續。

當用戶創建新級別的項目時,我可以設法在數據庫中創建表。 但是每個項目都需要一個特定的wordpress頁面。 當用戶進行新項目時,我很難創建一個頁面。 當未登錄Wordpress的用戶嘗試創建頁面時,這些人如何創建wordpress頁面?

簡而言之,我想在wordpress內創建一個頁面,方法是從wordpress外部單擊按鈕。 請確保用戶未登錄wordpress,他與wordpress無關。 這是過程發生在前端。如何使其成為可能?

我有完全相同的設置,並且我使用一種名為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”的新頁面,並為其分配new-project.php模板。

  5. 最后,您將需要通過在WordPress儀表板中的“設置>永久鏈接”並單擊保存更改來更新重寫規則以包括新的自定義帖子類型。

  6. 現在,當您的訪問者導航到“新項目”頁面(example.com/new-project)時,將創建一個新項目,並將其重定向以查看該項目(WordPress將使用我們創建的single-project.php來顯示數據)。這個項目)。

在主題目錄/文件夾中創建新的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(); ?>

之后,轉到管理面板創建新頁面。 將使用此模板名稱列出一個下拉列表。 您可以從那里選擇此模板。

將模板頁面testtemplate.php放在帶有注釋行的活動主題目錄中

/**
 * Template Name: Test Template
 */

然后,在管理面板上創建新頁面時,它將在選擇模板下拉列表中可用。

在這里您可以選擇相應的模板。

我認為您需要查看wp_insert_post() 此功能使您能夠動態創建帖子。 (此處的“帖子”是指任何類型的帖子/頁面/自定義帖子類型。)

我將創建一個自定義帖子類型(實際上是“自定義頁面類型”),並讓用戶使用wp_insert_post()創建一個新的帖子類型。 wp_insert_post()至少需要一個標題和一個內容,並通過清理傳遞數據(因此,它與創建用戶的帖子一樣安全)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM