Php Wordpress数据库查询并输出为HTML列表

Php Wordpress数据库查询并输出为HTML列表,php,sql,wordpress,shortcode,Php,Sql,Wordpress,Shortcode,为了在Wordpress数据库表中进行数据库查询,我正在尝试使用PHP代码。结果(一个特定列的所有不同值)应该放在HTML列表中。我希望能够将输出放在我的内容中的任何地方,因此它应该可以通过短代码访问 这是我到目前为止得到的。这段代码不会在wordpress或控制台中产生任何错误,但它也不起作用。。。所需位置没有显示任何内容 class my_Shortcode { var $echo; function __construct() { add_shortco

为了在Wordpress数据库表中进行数据库查询,我正在尝试使用PHP代码。结果(一个特定列的所有不同值)应该放在HTML列表中。我希望能够将输出放在我的内容中的任何地方,因此它应该可以通过短代码访问

这是我到目前为止得到的。这段代码不会在wordpress或控制台中产生任何错误,但它也不起作用。。。所需位置没有显示任何内容

class my_Shortcode {

    var $echo;

    function __construct() {
        add_shortcode( 'my_plugin', array( $this, 'shortcode' ) );
    }    

    function shortcode( $atts ) {
        global $wpdb;

        $table = isset( $atts['table'] ) ? $atts['table'] : false;
        $column = isset( $atts['column'] ) ? $atts['column'] : false;
        $listDisplay = isset( $atts['listDisplay'] ) ? $atts['listDisplay'] : false;

        if ( $listDisplay == true ) {
            if ( false != $table && false != $column ) {
                $this->echo = "";
                $results = $wpdb->get_results(
                    prepare('SELECT DISTINCT ' . $column . ' FROM ' . $table)
                );
                $this->echo .= "<div class=\"list\"><ul>";

                foreach ($results as $result) {
                    $this->echo .= "<li>$result</li>\n"; 
                }
                $this->echo .= "</ul></div>";
                return $this->echo;
            }
        }
    }
}
class my\u短码{
var$echo;
函数_u构造(){
添加_短代码('my_plugin',array($this,'shortcode'));
}    
函数短代码($atts){
全球$wpdb;
$table=isset($atts['table'])?$atts['table']:false;
$column=isset($atts['column'])?$atts['column']:false;
$listDisplay=isset($atts['listDisplay'])?$atts['listDisplay']:false;
如果($listDisplay==true){
if(false!=$table&&false!=$column){
$this->echo=“”;
$results=$wpdb->get_results(
准备('从'.$表中选择不同的'.$列'.)
);
$this->echo.=“
    ”; foreach($results作为$result){ $this->echo.=“
  • $result
  • \n”; } $this->echo.=“
”; 返回$this->echo; } } } }

我很高兴有任何建议

你喜欢这个吗

<?php
    echo do_shortcode("[my_plugin table='wp_posts' column=id]");
?>





class my_Shortcode {

    var $echo;

    function __construct() {
    add_shortcode( 'my_plugin', array( $this, 'shortcode' ) );
    }    

    function shortcode( $atts ) {
    global $wpdb;

    $table = isset( $atts['table'] ) ? $atts['table'] : false;
    $column = isset( $atts['column'] ) ? $atts['column'] : false;
    $listDisplay = isset( $atts['listDisplay'] ) ? $atts['listDisplay'] : true;

    if ( $listDisplay == true ) {
        if ( false != $table && false != $column ) {
        $this->echo = "";

        $results = $wpdb->get_results("SELECT DISTINCT  $column  FROM  $table ",ARRAY_A);


        $this->echo .= "<div class=\"list\"><ul>";

        foreach ($results as  $key => $result) {
            $keys = array_keys( $result);                   
            $this->echo .= '<li>'.$result[ $keys[0] ].'</li>\n'; 
        }
        $this->echo .= "</ul></div>";
        return $this->echo;
        }
    }
    }
}

$test = new my_Shortcode();