如何為WordPress外掛程式添加問答功能
WordPress是一款強大且靈活的內容管理系統,而外掛程式則為用戶提供了更多可自訂的功能。在建立一個活躍的社區或一個充滿互動的網站時,添加問答功能是一個非常好的選擇。本文將介紹如何為WordPress外掛程式添加問答功能,並給出一些程式碼範例。
- 建立自訂資料庫表格
問答功能需要一個地方來儲存問題和答案的資料。可以透過建立自訂的資料庫表來實現。以下是一個範例的MySQL資料庫表格結構:
CREATE TABLE wp_questions ( id INT AUTO_INCREMENT PRIMARY KEY, question VARCHAR(255), answer TEXT, user_id INT, create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
在WordPress中,可以使用dbDelta
函數來建立和更新資料庫表。下面是一個範例函數,用於建立問答表:
function create_question_table() { global $wpdb; $table_name = $wpdb->prefix . 'questions'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id INT AUTO_INCREMENT PRIMARY KEY, question VARCHAR(255), answer TEXT, user_id INT, create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); }
- #新增前台頁面範本
為了讓使用者提問和查看答案,需要建立對應的前台頁面範本。可以在主題的資料夾中建立一個名為template-qa.php
的文件,並在其中新增以下程式碼:
<?php /* Template Name: Q&A Template */ get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php while ( have_posts() ) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <h1 class="entry-title"><?php the_title(); ?></h1> </header> <div class="entry-content"> <?php the_content(); ?> </div> </article> <?php endwhile; ?> <?php global $wpdb; $table_name = $wpdb->prefix . 'questions'; $questions = $wpdb->get_results( "SELECT * FROM $table_name ORDER BY create_date DESC" ); foreach ( $questions as $question ) { echo '<h2 id="question-question">' . $question->question . '</h2>'; echo '<p>' . $question->answer . '</p>'; } ?> </main> </div> <?php get_sidebar(); get_footer(); ?>
在後台,建立一個新的頁面,並選擇Q&A Template
為頁面範本。
- 新增問題提交和答案提交的功能
在前台頁面中,新增一個表單,讓使用者可以提交問題。可以使用以下程式碼範例:
<form action="" method="post"> <label for="question">问题:</label> <input type="text" name="question" id="question"> <input type="submit" value="提交问题"> </form> <?php if ( isset( $_POST['question'] ) && $_POST['question'] != '' ) { global $wpdb; $table_name = $wpdb->prefix . 'questions'; $question = sanitize_text_field( $_POST['question'] ); $user_id = get_current_user_id(); $wpdb->insert( $table_name, array( 'question' => $question, 'user_id' => $user_id ) ); } ?>
同樣地,為了讓管理員或其他使用者可以回答問題,可以添加以下程式碼範例:
<h2 id="回答问题">回答问题:</h2> <form action="" method="post"> <textarea name="answer"></textarea> <input type="submit" value="提交答案"> </form> <?php if ( isset( $_POST['answer'] ) && $_POST['answer'] != '' ) { global $wpdb; $table_name = $wpdb->prefix . 'questions'; $answer = sanitize_textarea_field( $_POST['answer'] ); $question_id = 1; // 假设这是问题的ID $wpdb->update( $table_name, array( 'answer' => $answer ), array( 'id' => $question_id ) ); } ?>
請注意,這只是一個簡單的範例,還可以根據需要自行擴展。
透過以上步驟,就可以為WordPress外掛程式加入問答功能。希望這篇文章能對你有所幫助,祝你在建立一個活躍的社群或網站時取得成功!
以上是如何為WordPress外掛程式新增問答功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

你不需要編程知識就能使用WordPress,但掌握編程可以提升體驗。 1)使用CSS和HTML可以調整主題樣式。 2)PHP知識能編輯主題文件,添加功能。 3)自定義插件和元標籤可優化SEO。 4)注意備份和使用子主題以防更新問題。

TosecureaWordPresssite,followthesesteps:1)RegularlyupdateWordPresscore,themes,andpluginstopatchvulnerabilities.2)Usestrong,uniquepasswordsandenabletwo-factorauthentication.3)OptformanagedWordPresshostingorsecuresharedhostingwithawebapplicationfirewal

WordPressExcelSoverotherWeberSiteBuilderSduetoItsflexible,可伸縮性,andopen-sourcenature.1)它'saversatilecmswithExtEnsextEnsexenSiveCustomizedOptionsVIATHEMESANDPLUGINS.2)它的alllearbutoffersbutoffersbutoffersbutoffersbutefersbuterbutfulcontrololoncemastered.3)

2025年網站開發的七個必備WordPress插件 在2025年建立頂級WordPress網站需要速度,響應能力和可擴展性。 實現這種有效的實現通常取決於戰略插件的選擇。 這篇文章Highlig

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins

是的,wordpressisisexcellentforcortingaportfoliowebsite.1)itoffersnumeroversnumeroverportfolio-spificthemeslike'astra'astra'astra'astra'astra'astra'astra'astra'astra'elementor'Enelementor'enableIntiviveSiveSign,Thoughtemanycanslowthesite.3)

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke

WordPressIsAcmsDuetoItseAsofuse,自定義,USERMANAMECTION,SEO和COMMUNITYSUPPORT.1)ITSIMPLIFIESCONTENTMANGAMEWITHANINTUISIDERFEEFFECE.2)提供extentensiveCustomizationThroughThroughTheMesandPlugins.3)supportrobustuserrolesandplugins.4)supportrobustuserrolesandpermissions.4)增強


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。