Search code examples
phpwordpressbuddypress

PHP help with using variable inside code


I have this line of code: <?php global $bp; query_posts( 'author=$bp->displayed_user->id' ); if (have_posts()) : ?> but it doesn't work as expected. Probably because it's not grabbing the $bp->displayed_user->id part correctly. How do I do it?

Thanks


Solution

  • It isn't working because it's treating the 'author=$bp->displayed_user->id' as a string rather than inlining the contents of the variable. (This is the main difference between using single and double quotes. Have a read of the PHP strings manual page for more information.)

    To fix this, try either:

    query_posts('author=' . $bp->displayed_user->id);
    

    or

    query_posts("author={$bp->displayed_user->id}");
    

    That said, I'd personally recommend the first approach, as it's more explicit what's going on.