user1823055
user1823055

Reputation: 91

Filter Category Loop Dynamically

I'm trying to display a custom loop with a dynamic category filter.

As a setup I have categories of all usernames which are created once a user creates an account.

So I am trying to echo the user's username as a category filter. It works when I echo is elsewhere on the page, but it doesn't work when I try to embed it like so:

<?php query_posts('category_name=global $current_user; if ( isset($current_user) ) {echo $current_user->user_login;} &posts_per_page=10'); ?> &posts_per_page=6'); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>  
<?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?>

<?php endwhile; else: ?>

NO Posts Present 

<?php endif; ?>

Any help would be greatly appreciated, thanks.

Upvotes: 0

Views: 1063

Answers (1)

Matthew Blancarte
Matthew Blancarte

Reputation: 8301

Without addressing whether or not you should be using query_posts, you can try refactoring your query.

<?php
  global $current_user;
  $cat = (isset($current_user)) ? "category_name=$current_user->user_login&" : "";
  query_posts($cat . 'posts_per_page=6'); 
?>

You may want to read this documentation regarding query_posts, as well.

Upvotes: 1

Related Questions