Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress functions.php不通过快捷码返回结果_Php_Wordpress - Fatal编程技术网

Wordpress functions.php不通过快捷码返回结果

Wordpress functions.php不通过快捷码返回结果,php,wordpress,Php,Wordpress,在使用短代码时,让下面的代码(添加到theme的functions.php中)将结果返回到post有点困难 下面是我放在functions.php中的代码: <?php function foo_shortcode(){ global $wpdb; $r = $wpdb->get_results("SELECT * FROM shows_16 c

在使用短代码时,让下面的代码(添加到theme的functions.php中)将结果返回到post有点困难

下面是我放在functions.php中的代码:

<?php

function foo_shortcode(){

    global $wpdb; 

    $r = $wpdb->get_results("SELECT 
                *
                FROM 
                    shows_16 c
                WHERE
                    c.status = 1
                GROUP BY
                    c.id
                ORDER BY
                    c.date_start ASC");

    $c1= '<div id="main-wrapper">   

        <table cellspacing="0" cellpadding="0">
                <tbody>
                <tr id="r1">
                    <td id="thead" colspan="4">EVENTS 2016</td>
                </tr>
                <tr id="r2">
                    <td id="c1" >Date</td>
                    <td id="c2" >Event</td>
                    <td id="c3" >City</td>
                    <td id="c4" >Details</td>
                </tr>';

    foreach ($r as $show_result) {


        $c2 .= '<tr id="rx" itemscope itemtype="http://schema.org/Event"><td id="x1"><meta itemprop="startDate" content="' . 
            $show_result["date_start"] . '">' . date('M j', strtotime($show_result['date_start'])) .
            (($show_result["date_start"] != $show_result["date_end"]) ? print ' - ' . 
            '<meta itemprop="endDate" content="'. $show_result["date_end"].'">' . 
            date('M j', strtotime($show_result["date_end"])) : "") . '</td><td id="x2">' . 
            ($show_result['url']=='') ? print '<span itemprop="name">'.$show_result["name"].
            '</span>' : print '<a itemprop="url" href="' . $show_result["url"] . 
            '" target="_blank"><span itemprop="name">'. $show_result["name"] . 
            '</span></a>') . '</td> <td id="x3" itemprop="location" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="addressLocality">'. 
            $show_result["city"] . '</span></td> <td id="x4" itemprop="description">' .  
            $show_result["details"]. '</td></tr>';

    }


    $c3 = '</tbody></table></div>';

    $c4 = $c1.$c2.$c3;

    return $c4;
}


add_shortcode('foo_sc', 'foo_shortcode');

?>
我所期望的结果是一个包含类似以下事件的表:

Jan 3   The Foo Event   Bar-city    Foo's Event details go here
Jan 4   The Bar Event   Foo-city    Bar's Event details go here
...
ob_start();?>

//...create your HTML...
<?php
return ob_get_clean();
我在wordpress数据库中设置了表“shows_16”,它包含有效数据

函数返回的结果来自$c1、$c3,而不是$c2


感谢您的帮助。谢谢

您的
$c2
变量没有正确初始化,在循环外部无法访问。解决方案可能是在循环上方对其进行如下初始化

$c2 ="";
foreach ($r as $show_result) {


        $c2 .= '<tr id="rx" itemscope itemtype="http://schema.org/Event"><td id="x1"><meta itemprop="startDate" content="' . 
            $show_result["date_start"] . '">' . date('M j', strtotime($show_result['date_start'])) .
            (($show_result["date_start"] != $show_result["date_end"]) ? print ' - ' . 
            '<meta itemprop="endDate" content="'. $show_result["date_end"].'">' . 
            date('M j', strtotime($show_result["date_end"])) : "") . '</td><td id="x2">' . 
            ($show_result['url']=='') ? print '<span itemprop="name">'.$show_result["name"].
            '</span>' : print '<a itemprop="url" href="' . $show_result["url"] . 
            '" target="_blank"><span itemprop="name">'. $show_result["name"] . 
            '</span></a>') . '</td> <td id="x3" itemprop="location" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="addressLocality">'. 
            $show_result["city"] . '</span></td> <td id="x4" itemprop="description">' .  
            $show_result["details"]. '</td></tr>';

    }


    $c3 = '</tbody></table></div>';

    $c4 = $c1.$c2.$c3;
$c2=”“;
foreach($r作为$show\u结果){
$c2.=''.date('MJ',strottime($show_result['date_start']))。
($show_result[“date_start”]!=$show_result[“date_end”])?打印“-”。
'' . 
日期('M j',标准时间($show_result[“date_end”]):“”)。
($show_result['url']=='')打印“”。$show_result[“name”]。
“”:打印“”)。“”。
$show_result[“city”]”。
$show_result[“details”]。';
}
$c3='';
$c4=$c1.$c2.$c3;

我知道这是一个很难回答的问题,但这是我的答案:D

如果您希望输出很长,最好使用ob_start()和ob_get_clean()而不是简单地将字符串存储在变量中

ob_start():

ob_get_clean():

它的功能应该是这样的:

Jan 3   The Foo Event   Bar-city    Foo's Event details go here
Jan 4   The Bar Event   Foo-city    Bar's Event details go here
...
ob_start();?>

//...create your HTML...
<?php
return ob_get_clean();
ob_start();?>
//…创建您的HTML。。。

你检查过你的查询了吗?当你在phpmyadmin中尝试它时,它是否显示了结果?是的-我刚刚测试过它,它从表中返回了预期的结果集。请检查我的答案,这应该会对你有所帮助。