基于wordpress用户登录显示自定义表行

时间:2014-04-13 03:31:25

标签: php wordpress html-table rows

我已为本地团队上传了一张自定义比赛时间表。每行包括worpdress用户ID。我可以显示一个人登录的表格,但我只想显示标有用户ID的行。这样,当他们登录时,他们只看到他们的时间。

这是我必须显示的完整表格:


<?php

if (is_user_logged_in()):

    global $wpdb;
    $TIMES = $wpdb->get_results("SELECT * FROM TIMES;");


    echo "<table>";
    foreach($TIMES as $TIMES){
        echo "<tr>";
        echo "<td>".$TIMES->EVENT."</td>";
        echo "<td>".$TIMES->Race."</td>";
        echo "<td>".$TIMES->Time."</td>";
        echo "<td>".$TIMES->Meet."</td>";
        echo "<td>".$TIMES->Swimmer."</td>";
        echo "<td>".$TIMES->users_login."</td>";
        echo "</tr>";
    }
    echo "</table>";
else:
    echo "Sorry, only registered users can view this information";
endif;
?>

是否有一种基于用户登录显示行的简单方法?

1 个答案:

答案 0 :(得分:1)

尝试:

<?php

if (is_user_logged_in()):

    $userid = get_user_id();

    global $wpdb;
    $TIMES = $wpdb->get_results("SELECT * FROM TIMES WHERE userid = '$userid';");


    echo "<table>";
    foreach($TIMES as $TIMES){
        echo "<tr>";
        echo "<td>".$TIMES->EVENT."</td>";
        echo "<td>".$TIMES->Race."</td>";
        echo "<td>".$TIMES->Time."</td>";
        echo "<td>".$TIMES->Meet."</td>";
        echo "<td>".$TIMES->Swimmer."</td>";
        echo "<td>".$TIMES->users_login."</td>";
        echo "</tr>";
    }
    echo "</table>";
else:
    echo "Sorry, only registered users can view this information";
endif;
?>

其中

get_user_id()将返回当前的用户ID

例如

function get_user_id(){
    return $_SESSION['userid'];
}