在wordpress页面顶部显示短代码

时间:2014-10-12 03:51:05

标签: php wordpress shortcode

我有一个页面,可以从sql数据库中显示我的成员信息。我有一个短代码形式的输出,但信息显示在页面内容上方,即使短代码放在文本编辑器的最底部。我知道问题是由于这个代码回应而不是返回,但我是PHP的新手,需要语法帮助。

function listMembers() {
$sql1=mysql_query("select state_id,state_name from ".TABLE_STATE);
while($row1=mysql_fetch_assoc($sql1))
{
    ?> <a href="#" onclick="getDetails(
<?php echo $row1["state_id"];?>)"> <?php echo $row1["state_name"]; ?>

以下是所有代码     

$sql1=mysql_query("select state_id,state_name from ".TABLE_STATE);
while($row1=mysql_fetch_assoc($sql1))
{
    ?> <a href="#" onclick="getDetails(<?php echo $row1["state_id"];?>)"> <?php echo       $row1["state_name"]; ?> </a> &nbsp;<?php

}
?><br/><br/><div id="resultDiv"></div><?php

}
add_shortcode('memberlist', 'listMembers');

function listRescueStandards() {

$display_members = '';
$sql = mysql_query("SELECT vc.*, s.*, m.*
    FROM ".TABLE_COMPLIANCE." vc, ".TABLE_STATE." s, ".TABLE_MEMBERS." m
    WHERE vc.member_id = m.cid
    AND m.status = '1'
    AND m.state = s.state_abbr
    ORDER BY m.state, m.organization ASC");

while ($row = mysql_fetch_array($sql)) {
    $organization = stripslashes($row['organization']);

    if ($row['website']) {
        $link = "<a href='http://".$row['website']."' target='_blank'>";
        $endlink = "</a>";
    } else {
        $link = "";
        $endlink = "";
    }

    if($x!=$row['state_name']){
        $display_members .= "<br /><strong>".strtoupper($row['state_name'])."</strong><br />";
        $x = $row['state_name'];
    }

    $display_members .= $link.$organization.$endlink."<br />
       ".stripslashes($row['address'])." ".stripslashes($row['address2'])."<br />
       ".stripslashes($row['city']).", ".stripslashes($row['state'])."  ".$row['zip']."<br />";

    if ($row['contact_name']) $display_members .= "Contact:   ".stripslashes($row['contact_name']);
    if ($row['contact_title'])  $display_members .= ", ".stripslashes($row['contact_title']);
    if ($row['phone'])      $display_members .= "<br />Tel: ".stripslashes($row['phone']);
    if ($row['fax'])    $display_members .= "<br />Fax: ".stripslashes($row['fax']);
    if ($row['email'])  $display_members .= "<br />".$row['email'];
    if ($row['website'])        $display_members .= "<br /><a href='http://".$row['website']."' target='_blank'>".$row['website']."</a>";
    if ($row['year_est'])       $display_members .= "<br />Founded in ".$row['year_est'].".";
    if ($row['org501c3'] == "1")        $display_members .= "<br />This organization IS registered with the IRS as a 501(c)3.";
    if ($row['org501c3'] != "1")    $display_members .= "<br />This organization is NOT     registered with the IRS as a 501(c)3.";

    $display_members .= "<br /><br />";
}
        return "<div class='memberlist'>" . $display_members . "</div>";

}
add_shortcode('standardslist', 'listRescueStandards');
  ?>

提前感谢您的帮助!我很感激有人在看这个,如果你需要澄清,请告诉我!

1 个答案:

答案 0 :(得分:1)

好的,好的。根据{{​​3}}你想要返回一个值,而不是回显它。您可以使用shortcodes apiecho类型的代码转换为return类型。这需要简单地将所有echo语句包装在ob_start()ob_end_clean()

function listMembers() {
$sql1=mysql_query("select state_id,state_name from ".TABLE_STATE);
ob_start(); //send all future echo statements to the buffer instead of output

while($row1=mysql_fetch_assoc($sql1))
{
    ?> <a href="#" onclick="getDetails(<?php echo $row1["state_id"];?>)"> <?php echo       $row1["state_name"]; ?> </a> &nbsp;<?php

}
?><br/><br/><div id="resultDiv"></div><?php


$result=ob_get_clean(); //capture the buffer into $result
return $result; //return it, instead of echoing

}