我的基本php代码产生了意外的结果

时间:2011-10-28 作者:Dr Deo

我正在编写一个代码片段来显示帖子的附件,但对于每个用户来说,他应该只能看到自己的附件,而不能看到其他用户的附件

    $current_user=wp_get_current_user();

    //display all post attachments. see http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post
    $args = array(
    \'post_type\' => \'attachment\',
    \'numberposts\' => null,
    \'post_status\' => null,
    \'post_parent\' => $post->ID
    );
echo "<b><u>Uploads By me</u></b>"; 
echo "<ol>";
    $attachments = get_posts($args);
    if ($attachments) {

        foreach ($attachments as $attachment) {
            echo "<li>";
            the_attachment_link($attachment->ID, false);
            echo " by author ".$attachment->post_author ;//debug info
            echo " and Current user is ".$current_user->ID ;//debug info
            if($current_user->ID==$attachment->ID) echo " Matched!! ";//debug info
            echo "</li>";
        }
    }
echo "<ol>";
//end attacmnets
我将代码放在自定义页面模板中,如下图所示,语句if($current_user->ID==$attachment->ID) echo " Matched!! "; 失败,这就是我想要依赖的有选择地显示附件。请帮忙enter image description here

1 个回复
最合适的回答,由SO网友:EarnestoDev 整理而成

Maybe you meant:

if($current_user->ID == $attachment->post_author) echo " Matched!! ";
PS: <根据意外的结果,我认为你指的是“阴茎疾病”部分。。。对吧

结束