在 wordpress 自定义模板中显示插入的数据
Posted
技术标签:
【中文标题】在 wordpress 自定义模板中显示插入的数据【英文标题】:Display the inserted data in wordpress custom template 【发布时间】:2017-05-15 07:45:55 【问题描述】:您好,我正在开发一个文字新闻网站。我创建了一个自定义模板,其中我有一个表单和字段被插入到数据库(mysql)中。代码如下所示。Insert into database
现在我需要以table
或grid
的形式在前端显示已经插入数据库的数据。我怎么能做到这一点。
我需要这样的输出。
Ritual Name Ritual Active
---------------------------
Test1 | Y
Test2 | Y
任何帮助表示赞赏。
函数.php
function childtheme_style_andscripts()
//wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_script('ajax-function', get_stylesheet_directory_uri() . '/js/ajaxfunction.js', array('jquery'), '1.0', true );
wp_localize_script( 'ajax-function', 'usersubmitform', array(
'url'=> admin_url('admin-ajax.php'),
'security'=> wp_create_nonce('our-nonce')
) );
add_action('wp_enqueue_scripts','childtheme_style_andscripts');
function form_action_function()
require_once(dirname( __FILE__ ).'/../../../wp-load.php');
$data = $_POST['data'];
global $wpdb;
if( !check_ajax_referer('our-nonce', 'security' ) )
wp_send_json_error('security failed');
return;
//var_dump($data);
$rname=$data['rname'];
$ractive=$data['ractive'];
$table_name = "rituals";
$wpdb->insert($table_name, array ('Ritual_Name' => $rname, 'Ritual_Active' => $ractive) );
//$wpdb->show_errors();
//$wpdb->print_error();
echo 'From Submitted Successfully';
die();
add_action('wp_ajax_nopriv_form_action_function','form_action_function');
add_action('wp_ajax_form_action_function','form_action_function');
ajaxfunction.js
jQuery(document).ready(function($)
var submitButton = document.getElementById('usersubmit');
var ajaxFunctionformprocess = function(fromdata, action)
$.ajax(
type:'post',
url: usersubmitform.url,
data:
action:action,
data:fromdata,
security:usersubmitform.security,
,
success:function(reponse)
$('div.msg').html(reponse);
,
error:function(response)
alert(response);
);
submitButton.addEventListener('click', function(event)
event.preventDefault();
var fromdata =
'rname':document.getElementById('rname').value,
'ractive':document.getElementById('ractive').value,
;
ajaxFunctionformprocess(fromdata, 'form_action_function');
);
);
Samplepage.php(自定义模板)
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<h1 class="headingform">User Form</h1>
<div class="msg"></div>
<form class="userform">
Ritual Name: <input type="text" id="rname" name="rname" /><br> <br>
Ritual Active: <input type="text" id="ractive" name="ractive" /> <br><br>
<input id="usersubmit"type="submit" Value="Submit" />
</form>
</div><!-- #content -->
</div><!-- #primary -->
【问题讨论】:
***.com/questions/25065600/… 检查第二个答案 赏金未奖励 你能告诉我你在编辑器中真正需要什么吗? @VasimVanzara 在编辑器中,我需要一个包含许多细节的弹出表单。当我单击可视化编辑器中的按钮时,应该会出现弹出窗口。 【参考方案1】:案例一:从表格中创建展示信息的模板
<?php
/*
Template Name: Disply Ritural
Description: display ritual information in table formate
*/
global $wpdb;
$table_name = "rituals";
$result = $wpdb->get_results ("SELECT * FROM $table_name");
foreach ( $result as $ritual_info )
echo $ritual_info['Ritual_Name'];
echo $ritual_info['Ritual_Active'];
案例 2:创建一个易于使用的简码。打开您的 functions.php 文件在此处添加代码。
function show_ritual_info()
global $wpdb;
$table_name = "rituals";
$query="SELECT * FROM $table_name";
$result = $wpdb->get_results ($query);
foreach ( $result as $ritual_info )
echo $ritual_info['Ritual_Name'];
echo $ritual_info['Ritual_Active'];
add_shortcode('show_ritual_info','show_ritual_info');
如何使用短代码
php 文件内部
<?php echo do_shortcode('[show_ritual_info]'); ?>
内部管理页面
['show_ritual_info']
【讨论】:
我收到了这些警告Warning: strpos() expects parameter 1 to be string, array given in C:\xampp\htdocs\wordpress-4.7\wordpress\wp-includes\shortcodes.php on line 205 Warning: preg_match_all() expects parameter 2 to be string, array given in C:\xampp\htdocs\wordpress-4.7\wordpress\wp-includes\shortcodes.php on line 213 Warning: array_intersect(): Argument #2 is not an array in C:\xampp\htdocs\wordpress-4.7\wordpress\wp-includes\shortcodes.php on line 214 Array
我用的是案例2
在函数 .php 中我已经放置了这段代码function show_ritual_info() require_once(dirname( __FILE__ ).'/../../../wp-load.php'); global $wpdb; $table_name = "rituals"; $result = $wpdb->get_results ("SELECT * FROM $table_name"); $wpdb->show_errors(); $wpdb->print_error(); foreach ( $result as $ritual_info ) echo $ritual_info['Ritual_Name']; echo $ritual_info['Ritual_Active']; add_shortcode('show_ritual_info','show_ritual_info');
在自定义模板中我添加了这个<?php echo do_shortcode(['show_ritual_info']); ?>
function show_ritual_info() global $wpdb; $table_name = "rituals"; $result = $wpdb->get_results ("SELECT * FROM $table_name"); $wpdb->show_errors(); $wpdb->print_error(); //foreach ( $result as $ritual_info ) // echo $ritual_info['Ritual_Name']; echo $ritual_info['Ritual_Active']; // add_shortcode('show_ritual_info','show_ritual_info');
【参考方案2】:
类似这样的:
<?php
global $wpdb;
$table_name = "rituals";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<?php if( ! empty($retrieve_data) ):?>
<table>
<tr>
<th>Ritual Name</th>
<th>Ritual Active</th>
</tr>
<?php foreach ($retrieve_data as $retrieved_data): ?>
<tr>
<td><?php echo $retrieved_data['Ritual_Name'];?></td>
<td><?php echo $retrieved_data['Ritual_Active'];?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
不检查但必须工作。)
【讨论】:
以上是关于在 wordpress 自定义模板中显示插入的数据的主要内容,如果未能解决你的问题,请参考以下文章