簡體   English   中英

Wordpress functions.php不通過簡碼返回結果

[英]Wordpress functions.php not returning results via shortcode

使用短代碼時,讓以下代碼(添加到theme的functions.php中)將結果返回到帖子時遇到了一些麻煩。

這是我放入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');

?>

然后,我在Wordpress帖子中使用以下簡碼:

[foo_sc]

我期望的結果是一個表,其中填充了如下事件:

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
...

我在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;

我知道這是一個非常棘手的問題,但這是我的答案。 :D

如果您希望輸出較長,最好使用ob_start()和ob_get_clean()而不是簡單地將字符串存儲在變量中。

ob_start(): http//php.net/manual/zh/function.ob-start.php

ob_get_clean(): http//php.net/manual/zh/function.ob-get-clean.php

它應具有以下功能:

ob_start();?>

//...create your HTML...
<?php
return ob_get_clean();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM