我最近與我的一位客戶合作,她在她的工作領域擔任專業顧問。她問我是否可以實施一個問答系統,確切地說是一個常見問題解答頁面。我說,“當然,我們可以創建一個頁面,然後用不同的樣式粘貼問題和答案”,但她說她會創建不同的頁面並對問題和答案進行分類,為了更有條理,她需要一個不同的方法。
#為此,我將向您展示如何使用自訂貼文類型、分類法和短程式碼,透過幾行簡單的程式碼來處理她的請求。
建立常見問題解答系統需要什麼?
讓我們先建立自訂貼文類型。
當然,我們首先會為常見問題解答項目設定自訂貼文類型。我們將在register_post_type()
函數的幫助下建立一個新的自訂貼文類型,但如果您想要一個用於建立貼文類型的GUI,您可以使用GenerateWP 的貼文類型產生器工俱生成它,就像我在這個例子:
<?php if ( ! function_exists( 'tuts_faq_cpt' ) ) { // register custom post type function tuts_faq_cpt() { // these are the labels in the admin interface, edit them as you like $labels = array( 'name' => _x( 'FAQs', 'Post Type General Name', 'tuts_faq' ), 'singular_name' => _x( 'FAQ', 'Post Type Singular Name', 'tuts_faq' ), 'menu_name' => __( 'FAQ', 'tuts_faq' ), 'parent_item_colon' => __( 'Parent Item:', 'tuts_faq' ), 'all_items' => __( 'All Items', 'tuts_faq' ), 'view_item' => __( 'View Item', 'tuts_faq' ), 'add_new_item' => __( 'Add New FAQ Item', 'tuts_faq' ), 'add_new' => __( 'Add New', 'tuts_faq' ), 'edit_item' => __( 'Edit Item', 'tuts_faq' ), 'update_item' => __( 'Update Item', 'tuts_faq' ), 'search_items' => __( 'Search Item', 'tuts_faq' ), 'not_found' => __( 'Not found', 'tuts_faq' ), 'not_found_in_trash' => __( 'Not found in Trash', 'tuts_faq' ), ); $args = array( // use the labels above 'labels' => $labels, // we'll only need the title, the Visual editor and the excerpt fields for our post type 'supports' => array( 'title', 'editor', 'excerpt', ), // we're going to create this taxonomy in the next section, but we need to link our post type to it now 'taxonomies' => array( 'tuts_faq_tax' ), // make it public so we can see it in the admin panel and show it in the front-end 'public' => true, // show the menu item under the Pages item 'menu_position' => 20, // show archives, if you don't need the shortcode 'has_archive' => true, ); register_post_type( 'tuts_faq', $args ); } // hook into the 'init' action add_action( 'init', 'tuts_faq_cpt', 0 ); } ?>
提示:如果您的專案將涉及更多自訂貼文類型,這些類型可能比這個簡單的常見問題解答貼文類型更複雜,我可以推薦一個名為SuperCPT 的酷工具,它允許您創建新帖子類型具有更簡單的程式碼。我也寫了一篇關於SuperCPT的教程,你可以在這裡查看。
為了區分不同類型的問題(例如我的客戶關於流產和產後憂鬱症的問題和答案),我們需要一個類別系統。正如你們許多人已經知道的那樣,WordPress 透過自訂分類法提供了此功能。
這裡的基本函數是 register_taxonomy()
但同樣,如果您需要圖形介面,您可以使用GenerateWP 的分類產生器工具。
程式碼如下:
<?php if ( ! function_exists( 'tuts_faq_tax' ) ) { // register custom taxonomy function tuts_faq_tax() { // again, labels for the admin panel $labels = array( 'name' => _x( 'FAQ Categories', 'Taxonomy General Name', 'tuts_faq' ), 'singular_name' => _x( 'FAQ Category', 'Taxonomy Singular Name', 'tuts_faq' ), 'menu_name' => __( 'FAQ Categories', 'tuts_faq' ), 'all_items' => __( 'All FAQ Cats', 'tuts_faq' ), 'parent_item' => __( 'Parent FAQ Cat', 'tuts_faq' ), 'parent_item_colon' => __( 'Parent FAQ Cat:', 'tuts_faq' ), 'new_item_name' => __( 'New FAQ Cat', 'tuts_faq' ), 'add_new_item' => __( 'Add New FAQ Cat', 'tuts_faq' ), 'edit_item' => __( 'Edit FAQ Cat', 'tuts_faq' ), 'update_item' => __( 'Update FAQ Cat', 'tuts_faq' ), 'separate_items_with_commas' => __( 'Separate items with commas', 'tuts_faq' ), 'search_items' => __( 'Search Items', 'tuts_faq' ), 'add_or_remove_items' => __( 'Add or remove items', 'tuts_faq' ), 'choose_from_most_used' => __( 'Choose from the most used items', 'tuts_faq' ), 'not_found' => __( 'Not Found', 'tuts_faq' ), ); $args = array( // use the labels above 'labels' => $labels, // taxonomy should be hierarchial so we can display it like a category section 'hierarchical' => true, // again, make the taxonomy public (like the post type) 'public' => true, ); // the contents of the array below specifies which post types should the taxonomy be linked to register_taxonomy( 'tuts_faq_tax', array( 'tuts_faq' ), $args ); } // hook into the 'init' action add_action( 'init', 'tuts_faq_tax', 0 ); } ?>
就是這樣!現在您有了一個常見問題解答帖子類型,其中的分類法稱為“常見問題解答類別”,相互連結!檢查您的管理面板,您將在「常見問題」下看到「常見問題解答類別」選單項目。
就像常規貼文類別一樣,您可以在「常見問題解答類別」頁面中新增、編輯或刪除它們,也可以在編寫新的常見問題解答專案時新增類別。
[faq]
簡碼有趣的部分來了:建立短程式碼。 (如果您讀過我之前的帖子,您就會知道我是 WordPress 短代碼的忠實粉絲。)我們基本上會將常見問題解答項目嵌入到帖子和頁面中。
接下來會發生什麼:
讓我們開始建立短程式碼。與上面的程式碼一樣,我將添加一些有用的註解:
<?php if ( ! function_exists( 'tuts_faq_shortcode' ) ) { function tuts_faq_shortcode( $atts ) { extract( shortcode_atts( array( // category slug attribute - defaults to blank 'category' => '', // full content or excerpt attribute - defaults to full content 'excerpt' => 'false', ), $atts ) ); $output = ''; // set the query arguments $query_args = array( // show all posts matching this query 'posts_per_page' => -1, // show the 'tuts_faq' custom post type 'post_type' => 'tuts_faq', // show the posts matching the slug of the FAQ category specified with the shortcode's attribute 'tax_query' => array( array( 'taxonomy' => 'tuts_faq_tax', 'field' => 'slug', 'terms' => $category, ) ), // tell WordPress that it doesn't need to count total rows - this little trick reduces load on the database if you don't need pagination 'no_found_rows' => true, ); // get the posts with our query arguments $faq_posts = get_posts( $query_args ); $output .= '<div class="tuts-faq">'; // handle our custom loop foreach ( $faq_posts as $post ) { setup_postdata( $post ); $faq_item_title = get_the_title( $post->ID ); $faq_item_permalink = get_permalink( $post->ID ); $faq_item_content = get_the_content(); if( $excerpt == 'true' ) $faq_item_content = get_the_excerpt() . '<a href="' . $faq_item_permalink . '">' . __( 'More...', 'tuts_faq' ) . '</a>'; $output .= '<div class="tuts-faq-item">'; $output .= '<h3 class="tuts-faq-item-title">' . $faq_item_title . '</h3>'; $output .= '<div class="tuts-faq-item-content">' . $faq_item_content . '</div>'; $output .= '</div>'; } wp_reset_postdata(); $output .= '</div>'; return $output; } add_shortcode( 'faq', 'tuts_faq_shortcode' ); } ?>
就是這樣!現在我們有一個簡潔的短代碼來嵌入我們的問題和答案。您可以使用類別名稱tuts-faq
、tuts-faq-item
、tuts-faq-item-title
和tuts-faq-item- content
將其樣式設定。不過,即使您不包含額外的樣式,也應該沒問題。
由於這些程式碼不僅涉及前端樣式,還引入新功能,因此它算是外掛領域。這就是為什麼我們必須將程式碼保存為插件。當我們這樣做時,我們還應該在啟用和停用時刷新重寫規則。
完整程式碼如下:
<?php /* Plugin Name: Simple FAQ System Plugin URI: http://code.tutsplus.com/ Description: Helps you create an FAQ section for your WordPress website. Shortcode usage: <code>[faq]</code> Version: 1.0 Author: Barış Ünver Author URI: http://hub.tutsplus.com/authors/baris-unver License: Public Domain */ if ( ! function_exists( 'tuts_faq_cpt' ) ) { // register custom post type function tuts_faq_cpt() { // these are the labels in the admin interface, edit them as you like $labels = array( 'name' => _x( 'FAQs', 'Post Type General Name', 'tuts_faq' ), 'singular_name' => _x( 'FAQ', 'Post Type Singular Name', 'tuts_faq' ), 'menu_name' => __( 'FAQ', 'tuts_faq' ), 'parent_item_colon' => __( 'Parent Item:', 'tuts_faq' ), 'all_items' => __( 'All Items', 'tuts_faq' ), 'view_item' => __( 'View Item', 'tuts_faq' ), 'add_new_item' => __( 'Add New FAQ Item', 'tuts_faq' ), 'add_new' => __( 'Add New', 'tuts_faq' ), 'edit_item' => __( 'Edit Item', 'tuts_faq' ), 'update_item' => __( 'Update Item', 'tuts_faq' ), 'search_items' => __( 'Search Item', 'tuts_faq' ), 'not_found' => __( 'Not found', 'tuts_faq' ), 'not_found_in_trash' => __( 'Not found in Trash', 'tuts_faq' ), ); $args = array( // use the labels above 'labels' => $labels, // we'll only need the title, the Visual editor and the excerpt fields for our post type 'supports' => array( 'title', 'editor', 'excerpt', ), // we're going to create this taxonomy in the next section, but we need to link our post type to it now 'taxonomies' => array( 'tuts_faq_tax' ), // make it public so we can see it in the admin panel and show it in the front-end 'public' => true, // show the menu item under the Pages item 'menu_position' => 20, // show archives, if you don't need the shortcode 'has_archive' => true, ); register_post_type( 'tuts_faq', $args ); } // hook into the 'init' action add_action( 'init', 'tuts_faq_cpt', 0 ); } if ( ! function_exists( 'tuts_faq_tax' ) ) { // register custom taxonomy function tuts_faq_tax() { // again, labels for the admin panel $labels = array( 'name' => _x( 'FAQ Categories', 'Taxonomy General Name', 'tuts_faq' ), 'singular_name' => _x( 'FAQ Category', 'Taxonomy Singular Name', 'tuts_faq' ), 'menu_name' => __( 'FAQ Categories', 'tuts_faq' ), 'all_items' => __( 'All FAQ Cats', 'tuts_faq' ), 'parent_item' => __( 'Parent FAQ Cat', 'tuts_faq' ), 'parent_item_colon' => __( 'Parent FAQ Cat:', 'tuts_faq' ), 'new_item_name' => __( 'New FAQ Cat', 'tuts_faq' ), 'add_new_item' => __( 'Add New FAQ Cat', 'tuts_faq' ), 'edit_item' => __( 'Edit FAQ Cat', 'tuts_faq' ), 'update_item' => __( 'Update FAQ Cat', 'tuts_faq' ), 'separate_items_with_commas' => __( 'Separate items with commas', 'tuts_faq' ), 'search_items' => __( 'Search Items', 'tuts_faq' ), 'add_or_remove_items' => __( 'Add or remove items', 'tuts_faq' ), 'choose_from_most_used' => __( 'Choose from the most used items', 'tuts_faq' ), 'not_found' => __( 'Not Found', 'tuts_faq' ), ); $args = array( // use the labels above 'labels' => $labels, // taxonomy should be hierarchial so we can display it like a category section 'hierarchical' => true, // again, make the taxonomy public (like the post type) 'public' => true, ); // the contents of the array below specifies which post types should the taxonomy be linked to register_taxonomy( 'tuts_faq_tax', array( 'tuts_faq' ), $args ); } // hook into the 'init' action add_action( 'init', 'tuts_faq_tax', 0 ); } if ( ! function_exists( 'tuts_faq_shortcode' ) ) { function tuts_faq_shortcode( $atts ) { extract( shortcode_atts( array( // category slug attribute - defaults to blank 'category' => '', // full content or excerpt attribute - defaults to full content 'excerpt' => 'false', ), $atts ) ); $output = ''; // set the query arguments $query_args = array( // show all posts matching this query 'posts_per_page' => -1, // show the 'tuts_faq' custom post type 'post_type' => 'tuts_faq', // show the posts matching the slug of the FAQ category specified with the shortcode's attribute 'tax_query' => array( array( 'taxonomy' => 'tuts_faq_tax', 'field' => 'slug', 'terms' => $category, ) ), // tell WordPress that it doesn't need to count total rows - this little trick reduces load on the database if you don't need pagination 'no_found_rows' => true, ); // get the posts with our query arguments $faq_posts = get_posts( $query_args ); $output .= '<div class="tuts-faq">'; // handle our custom loop foreach ( $faq_posts as $post ) { setup_postdata( $post ); $faq_item_title = get_the_title( $post->ID ); $faq_item_permalink = get_permalink( $post->ID ); $faq_item_content = get_the_content(); if( $excerpt == 'true' ) $faq_item_content = get_the_excerpt() . '<a href="' . $faq_item_permalink . '">' . __( 'More...', 'tuts_faq' ) . '</a>'; $output .= '<div class="tuts-faq-item">'; $output .= '<h2 class="faq-item-title">' . $faq_item_title . '</h2>'; $output .= '<div class="faq-item-content">' . $faq_item_content . '</div>'; $output .= '</div>'; } wp_reset_postdata(); $output .= '</div>'; return $output; } add_shortcode( 'faq', 'tuts_faq_shortcode' ); } function tuts_faq_activate() { tuts_faq_cpt(); flush_rewrite_rules(); } register_activation_hook( __FILE__, 'tuts_faq_activate' ); function tuts_faq_deactivate() { flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'tuts_faq_deactivate' ); ?>
當我向我的客戶展示如何使用它時,她對結果感到滿意。但在這裡,我們可以使用更多功能擴充程式碼,例如...
posts_per_page
參數來限制帖子數量自訂短代碼的查詢,並使用wp_reset_postdata();
程式碼在行下方新增分頁連結所需的程式碼。請記住刪除 'no_found_rows' => true,
行,但如果不刪除它,分頁將無法運作! posts_per_page
參數從-1
更改為1
並新增另一行程式碼'orderby' => 'random',
和你'一切順利! 這就是您如何透過使用自訂文章類型、自訂分類法和短程式碼在 WordPress 中建立簡單的常見問題解答系統。我希望您喜歡本教程,並且可以在下一個專案中使用它。如果您喜歡這篇文章,請不要忘記分享它!
您對改進此常見問題解答系統有什麼想法嗎?在下面分享您的評論!
以上是使用自訂文章類型透過 WordPress 建立自訂常見問題解答系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!