2016-07-07 4 views
0

私は、原因をサポートすることを約束した人々のためのデータを含むカスタムテーブルを含むワードプレスサイトを持っています。アクティブなプレッガーの総数を表示するために任意のページにショートコードをドロップするプラグインを作成する必要があります。私は今、$ count_pledgersを経由して結果を出力する機能を組み込む方法を苦労していますデータベーステーブルから合計行を呼び出し、ショートコードを使用してサイトに存在するWordpressカスタムプラグイン

<?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 
*/ 
?> 

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); 

が、私はこのような単純なプラグインを作成することができます。

データベースクエリは単純です。ショートコード[pledgers_result]

答えて

1

まあ、実際のショートコードを追加する必要があります。それはかなり簡単です。ここDocumenation: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()関数の戻り値。

+0

親愛なる@leepowers、今日の残りの部分を取る。 –

関連する問題