[英]Wordpress custom plugin to call total rows from database table and present in site using a shortcode
我有一个wordpress网站,其中包含一个自定义表格,其中包含已承诺支持某项事业的人们的数据。 我需要创建一个插件,使我可以在任何页面上放置简码,以显示活动质押者的总数。
数据库查询很简单:
global $wpdb;
$pledgers = $wpdb->get_results("SELECT `business_name` FROM wp_x_pledgers WHERE business_name != '' AND active = '1' ORDER BY business_name;");
$count_pledgers = count($pledgers);
我可以创建一个简单的插件,如下所示:
<?php
/*
Plugin Name: Pledge Counter Plugin
Plugin URI: http://www.example.org/
Description: A plugin that tallies up active pledgers and presents the figure onscreen via a shortcode
Version: 1.0
Author: John Doe
Author URI: http://www.example.org/
License: GPL2
*/
?>
我现在正在努力如何包括通过短代码(例如[pledgers_result])输出$ count_pledgers结果的功能。
好了,您需要添加一个实际的简码。 这很简单。 此处的文档: https ://codex.wordpress.org/Shortcode_API
例:
<?php
/*
Plugin Name: Pledge Counter Plugin
Plugin URI: http://www.example.org/
Description: A plugin that tallies up active pledgers and presents the figure onscreen via a shortcode
Version: 1.0
Author: John Doe
Author URI: http://www.example.org/
License: GPL2
*/
// Shortcode render function
function sc_pledgers_result($atts) {
global $wpdb;
$pledgers = $wpdb->get_results("SELECT `business_name` FROM wp_x_pledgers WHERE business_name != '' AND active = '1' ORDER BY business_name;");
$count_pledgers = count($pledgers);
return "Pledgers count: $count_pledgers";
}
// Add shortcode to WordPress
add_shortcode("pledgers_result", "sc_pledgers_result");
只要激活了插件, [pledgers_result]
简码就会输出sc_pledgers_result()
函数的返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.